<Help> Simple Pause Needed.

D

da Vinci

OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

I beleive that I am looking for is something along the lines of a....

cin.get

but do not know the exact syntax for it.

I know it is so simple.... I can make and call functions with eyes
closed yet this illudes me...

Thanks.
 
M

Mike Wahler

da Vinci said:
OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

You can get almost that. With most systems with keyboards,
input is 'line buffered', so your program doesn't see any
input until the user presses the 'Enter' (aka 'return') key.
This lets one edit the input before submitting it to the
program. Think 'fumble-fingers', and 'backspace key' :)

So for such systems, you can have a 'Press Enter to continue',
in a few different ways.

You could use cin.get(), but I'd only use that as a prelude
to the program's termination, since even though 'get()' only
extracts a single character, the user could easily type in
e.g. ten characters (or more), and those characters would
still be waiting in a 'buffer', and be presented to the
next input request. So you'd need extra code to discard them.
(cin.ignore() etc.)

I find the most useful general purpose way to do this is
with the (nonmember) 'std::getline()' function, which will
extract all characters up to the first newline character, and
store them in a std::string object. (The newline character itself
is extracted but not stored). Then just throw the string away,
or give it to your cat. :)

Example:

#include <iostream>
#include <string>

void wait4user()
{
std::string response;
std::cout << "Press Enter to continue";
std::getline(std::cin, response);
}

int main()
{
std::cout << "Hello";
wait4user();
std::cout << "world\n";
wait4user();
return 0;
}

I beleive that I am looking for is something along the lines of a....

cin.get

cin.get() would work, but has the disadvantage I cited above.
but do not know the exact syntax for it.

That's what books are for. :) Any good C++ compiler
will also provide documentation for the standard library.
I know it is so simple.... I can make and call functions with eyes
closed yet this illudes me...

Open your eyes. :)

HTH,
-Mike
 
D

da Vinci

Excellent!

It worked like a charm.

I didnt know that info about the cin.get and having characters in the
buffer. Appreciate that! I bet that will save a great deal of heart
ache in the future.

Aw, come on.... keeping your eyes open during the ride is no fun!! :)

Thanks again.
 
J

Jonathan Mcdougall

OK, this has got to be a simple one and yet I cannot find the answer
in my textbook.

How can I get a simple pause after an output line, that simply waits
for any key to be pressed to move on?

Basically: "Press any key to continue..."

I beleive that I am looking for is something along the lines of a....

cin.get

but do not know the exact syntax for it.

There is no way in standard C++ (which is the topic of this newsgroup)
to do something like that. The closest thing would be

std::string dummy;
std::cin >> dummy;

which will wait for <enter>. If you want to detect a key without <enter>,
you will have to use something from your implementation. For further
informations, please ask in a newsgroup supporting your compiler.
 
J

Jonathan Mcdougall

OK, this has got to be a simple one and yet I cannot find the answer
There is no way in standard C++ (which is the topic of this newsgroup)
to do something like that. The closest thing would be

std::string dummy;
std::cin >> dummy;

which will wait for <enter>. If you want to detect a key without <enter>,
you will have to use something from your implementation. For further
informations, please ask in a newsgroup supporting your compiler.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9


Jonathan (pressed send to early)
 
M

Mike Wahler

Jonathan Mcdougall said:
There is no way in standard C++ (which is the topic of this newsgroup)
to do something like that. The closest thing would be

std::string dummy;
std::cin >> dummy;

Warning:

This might be sufficient for some circumstances, but
note that there could be a problem.

If this construct is used in a place before subsequent
input is requested, and if in response to the above,
the user supplies input with embedded whitespace,
e.g. "OK I did", only the "OK" will be extracted,
and "I did" is still there waiting, and will be supplied
to the next input.
Use std::getline() instead.
which will wait for <enter>. If you want to detect a key without <enter>,
you will have to use something from your implementation. For further
informations, please ask in a newsgroup supporting your compiler.

Agreed.

-Mike
 
M

Mike Wahler

da Vinci said:
Excellent!

It worked like a charm.

I didnt know that info about the cin.get

Start reading, my friend. :)
and having characters in the
buffer.

Perhaps I was not clear enough, but note that my
remarks about 'buffer' were only a generalization
about 'most systems' you're likely to encounter.

From the program's perspective, this issue is not
limited to just cin.get() but to all input operations.
The reason what I showed works, is that by definition,
std::getline() reads everything on a whole line,
and most systems with keyboards will buffer a line
at a time.

The language itself knows nothing about such operating
system buffers, and only 'sees' one character at a time
(The OS controls and supplies these characters to your
program).
Appreciate that! I bet that will save a great deal of heart
ache in the future.

Glad I could help.

BTW please don't top post. If you don't know what I'm
talking about, see the link Kevin provided.

-Mike
 
D

da Vinci

Start reading, my friend. :)

I tried. :) My textbook isn't very good, IMO, and the Schildt C/C++ SE
reference manual does cover get() and getline(), but not to the point
where I could get what I wanted out of it with my 1 month of C++
experiance. This sure isnt Turbo Pascal. :)
BTW please don't top post. If you don't know what I'm
talking about, see the link Kevin provided.

Had no idea, but now that I read the FAQ link, I will ensure I do not
do it any longer. I must have missed it the first time I read through
it.

Thanks.
 
D

da Vinci

Glad I could help.

Just in case you wanted to see how I used it, this is the program I
was working on. Way beyond what the assignment required, but I have
always gone above and beyond.

Hopefully I am posting the code right. I read the FAQ portion of it
and hope I get it right. (I hate being the new guy!!) I cut out alot
of the white space to save room. This is not how I format everything
in the code.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// Declare Functions Here

int Display_Main_Menu (int);
void View_Totals (int, int, int, int, int);
int Check_Number (int);
void Wait ();
void Final_Data (int, int, int, int, int);

int main (int)
{

// Variables for various program functions

int day=1, prod_num, quantity_today=0;
int prod_1=0, prod_2=0, prod_3=0, prod_4=0, prod_5=0;
int main_selection, good_or_bad=1;

// Variables for continue statements

char cont='N';
char day_done = 'N';

while ( day <= 7 )
{

main_selection = Display_Main_Menu (day); // Calls Function

switch (main_selection)
{
case 1: // Input Data - While & Switch Loops
system ("cls"); // Clear Screen Command
cont = 'N'; // Must have to avoid infinite looping!!!
while (cont != 'Y' )
{
while ( good_or_bad != 0 )
{
cout << "\n\n\nEnter product number (1-5): ";
cin >> prod_num;
good_or_bad = Check_Number(prod_num);
} // Ends Inner While

good_or_bad = 1; // Reset it to avoid infinite looping!!!

cout << "Enter quantity sold on day " << day << ": ";
cin >> quantity_today;
cout << "\n\nYou sold " << quantity_today
<< " units of product " << prod_num << ".";
cout << "\nIs this correct (Y/N)?";
cin >> cont;

if ( cont == 'y' ) // Sets upper case letter
cont = 'Y';
} // Ends Middle While

switch (prod_num) // Add Quantity to Product Count
{

case 1:
prod_1 = prod_1 + quantity_today;
break;
case 2:
prod_2 = prod_2 + quantity_today;
break;
case 3:
prod_3 = prod_3 + quantity_today;
break;
case 4:
prod_4 = prod_4 + quantity_today;
break;
case 5:
prod_5 = prod_5 + quantity_today;
break;
} // Ends Inner Switch

break;

case 2: // User Selected "View Totals" - Call Function
View_Totals (prod_1, prod_2, prod_3, prod_4, prod_5);
break;

case 3: // User Wants to Advanced One Day
day++;
break;

case 4: // User Wants to End Input Session
day = 8;
break;

} // Ends Outer Switch

} // Ends While

// Display the final sales report from the data entered.
Final_Data (prod_1, prod_2, prod_3, prod_4, prod_5);

return 0;
} // Ends Main

/*This function will be called to display the main menu
for the user. The variable passed out of this function
tells the *main* what menu the user wishes to access.*/

int Display_Main_Menu (int daynum)
{

int selection;

system ("cls"); // Clear Screen Command

cout << "\n\n\nDay " << daynum;
cout << "\n\n*****MAIN MENU*****";
cout << "\n\n1: Enter Data";
cout << "\n2: View Current Totals";
cout << "\n3: Continue to day " << ( daynum + 1 );
cout << "\n4: End Input Session";
cout << "\n\n\nSelection: ";
cin >> selection;

return selection;

}

/*This function will be called to display the totals currently
on file from previous input. There is no value returned from
this function.*/

void View_Totals(int prod1,int prod2,int prod3,int prod4,int prod5)
{
system ("cls"); // Clear Screen Command
cout << "\n\n\nYour current totals are:" << endl;
cout << "\nProduct 1: " << prod1 << endl;
cout << "Product 2: " << prod2 << endl;
cout << "Product 3: " << prod3 << endl;
cout << "Product 4: " << prod4 << endl;
cout << "Product 5: " << prod5 << endl;

Wait(); // Creates a "Press Enter to continue" statement
}

/* This function determines whether the input product number is
correct. The value must be an integer between 1 and 5. If it is
valid, then a value of 0 is returned. If it is invalid, then a
value of 1 is returned.*/

int Check_Number(int prodnum)
{
int return_this;

switch (prodnum)
{
case 1:
case 2:
case 3:
case 4:
case 5:
return_this = 0;
break;
default:
return_this = 1;
break;
}
return return_this;
}


/* This function will use a string to create a pause until the user
hits the enter key. Any other characters entered will be ignored.*/

void Wait()
{
string response;
cout << "Press Enter to continue";
getline(cin, response);
}

/* This function will have the number of products passed into it.
From this data, it will determine the amount of each product sold
and total gross sales for the week. Then, it will display the data.*/

void Final_Data(int prod1,int prod2,int prod3,int prod4,int prod5)
{
double p1total, p2total, p3total, p4total, p5total, gross;

p1total = (prod1 * 2.98);
p2total = (prod2 * 4.50);
p3total = (prod3 * 9.98);
p4total = (prod4 * 4.49);
p5total = (prod5 * 6.87);
gross = (p1total + p2total + p3total + p4total + p5total);

system ("cls"); // Clear Screen

cout << "\n\n\nYour final sales report for this week is as follows:";

cout << "\n\nProduct Number\tAmount Sold\tTotal Income";
cout << "\n1\t\t" << prod1 << "\t\t" << "$" << p1total;
cout << "\n2\t\t" << prod2 << "\t\t" << "$" << p2total;
cout << "\n3\t\t" << prod3 << "\t\t" << "$" << p3total;
cout << "\n4\t\t" << prod4 << "\t\t" << "$" << p4total;
cout << "\n5\t\t" << prod5 << "\t\t" << "$" << p5total;

cout << "\n\nYour total gross sales for this week is $" << gross
<< "\n\n\n";
}
 
K

Kevin Goodsell

da said:
I tried. :) My textbook isn't very good, IMO, and the Schildt C/C++ SE
reference manual does cover get() and getline(),

From the alt.comp.lang.learn.c-c++ FAQ
(http://www.faqs.org/faqs/C-faq/learn/):

13: What should I look for when picking a book to learn from?

[...]
Many C and C++ experts recommend against using ANY book written by
a certain Herbert Schildt. To see why, read the answer to question
16. [...]

16: Why do many experts not think very highly of Herbert Schildt's
books?

A good answer to this question could fill a book by itself. While
no book is perfect, Schildt's books, in the opinion of many
gurus, seem to positively aim to mislead learners and encourage
bad habits. Schildt's beautifully clear writing style only makes
things worse by causing many "satisfied" learners to recommend his
books to other learners.

Do take a look at the following scathing articles before deciding
to buy a Schildt text.
http://www.lysator.liu.se/c/schildt.html
http://herd.plethora.net/~seebs/c/c_tcr.html

The above reviews are admittedly based on two of Schildt's older
books. However, the language they describe has not changed in the
intervening period, and several books written at around the same
time remain highly regarded.

The following humorous post also illustrates the general feeling
towards Schildt and his books.
http://www.qnx.com/~glen/deadbeef/2764.html

There is exactly one and ONLY one C book bearing Schildt's name on
its cover that is at all recommended by many C experts - see Q 25.

-Kevin
 
J

Jonathan Mcdougall

Just in case you wanted to see how I used it, this is the program I
was working on. Way beyond what the assignment required, but I have
always gone above and beyond.

That is how we learn.
Hopefully I am posting the code right. I read the FAQ portion of it
and hope I get it right. (I hate being the new guy!!)
:)

I cut out alot
of the white space to save room.

Don't! This code is very hard to read. Do not use tabs since some
newsreaders (as mine) remove them, but at least indent by one or
two spaces.
This is not how I format everything
in the code.

I hope so, if not, don't tell us your name :)

The program in general is quite cool. A couple of guidelines though :

1) give better names to your variables and functions
2) try to break your code a bit more; a function must have a
representative name. If the name is too general (such as do_this() ),
break it until its name represents exactly what it does.
3) too much comment is like not enough
4) learn about arrays and vectors
5) don't give up!

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

// Declare Functions Here

int Display_Main_Menu (int);
void View_Totals (int, int, int, int, int);
int Check_Number (int);
void Wait ();
void Final_Data (int, int, int, int, int);

<personal opinion>
take the habit of naming the variables even on declaration,
it makes things clearer for everybody
int main (int)

Nope. main() has two allowed forms :

int main()

or

int main(int argc, char **argv)

where the names are optional.
{

// Variables for various program functions

int day=1, prod_num, quantity_today=0;
int prod_1=0, prod_2=0, prod_3=0, prod_4=0, prod_5=0;
int main_selection, good_or_bad=1;

take the habit of defining one variable per line and
to comment each of them. "various program functions" is not
very clear imo :)
// Variables for continue statements

char cont='N';
char day_done = 'N';

while ( day <= 7 )
{

main_selection = Display_Main_Menu (day); // Calls Function

Display_Main_Menu has a misleading name. It does not
only display, it asks for an option and returns it. Either
rename your function or (preferrably) break it in two.
switch (main_selection)
{
case 1: // Input Data - While & Switch Loops
system ("cls"); // Clear Screen Command

non portable, be careful. A system without 'cls' will do
nothing. On my system, 'cls' from the command prompt formats
the disk without warning. its a good thing I did not run it :)
cont = 'N'; // Must have to avoid infinite looping!!!
while (cont != 'Y' )
{
while ( good_or_bad != 0 )

a variable named good_or_bad does not inform on its use..
{
cout << "\n\n\nEnter product number (1-5): ";
cin >> prod_num;
good_or_bad = Check_Number(prod_num);
} // Ends Inner While

This kind of comment is useless and only clutters the code.
good_or_bad = 1; // Reset it to avoid infinite looping!!!

cout << "Enter quantity sold on day " << day << ": ";
cin >> quantity_today;
cout << "\n\nYou sold " << quantity_today
<< " units of product " << prod_num << ".";
cout << "\nIs this correct (Y/N)?";
cin >> cont;

if ( cont == 'y' ) // Sets upper case letter
cont = 'Y';

look out std::toupper()
} // Ends Middle While

switch (prod_num) // Add Quantity to Product Count
{

case 1:
prod_1 = prod_1 + quantity_today;
break;
case 2:
prod_2 = prod_2 + quantity_today;
break;
case 3:
prod_3 = prod_3 + quantity_today;
break;
case 4:
prod_4 = prod_4 + quantity_today;
break;
case 5:
prod_5 = prod_5 + quantity_today;
break;
} // Ends Inner Switch

mmm.. what about an array or a std::vector here (or even a std::map)?
If you didn't learn about them, that's ok, but know that it would
be a lot simpler.

Make the previous case in a seperate function.
case 2: // User Selected "View Totals" - Call Function
View_Totals (prod_1, prod_2, prod_3, prod_4, prod_5);
break;

case 3: // User Wants to Advanced One Day
day++;
break;

case 4: // User Wants to End Input Session
day = 8;
break;

} // Ends Outer Switch

Every case should look like these 3 : calling a function or two,
but that's it. If not, well.. it makes it harder to understand.
} // Ends While

// Display the final sales report from the data entered.
Final_Data (prod_1, prod_2, prod_3, prod_4, prod_5);

return 0;
} // Ends Main

/*This function will be called to display the main menu
for the user. The variable passed out of this function
tells the *main* what menu the user wishes to access.*/

int Display_Main_Menu (int daynum)
{

int selection;

system ("cls"); // Clear Screen Command

non portable
cout << "\n\n\nDay " << daynum;
cout << "\n\n*****MAIN MENU*****";
cout << "\n\n1: Enter Data";
cout << "\n2: View Current Totals";
cout << "\n3: Continue to day " << ( daynum + 1 );
cout << "\n4: End Input Session";
cout << "\n\n\nSelection: ";

Use std::endl instead of \n, which is not guaranteed to work
everywhere.
cin >> selection;

return selection;

}

/*This function will be called to display the totals currently
on file from previous input. There is no value returned from
this function.*/

void View_Totals(int prod1,int prod2,int prod3,int prod4,int prod5)
{
system ("cls"); // Clear Screen Command

not portable
cout << "\n\n\nYour current totals are:" << endl;
cout << "\nProduct 1: " << prod1 << endl;
cout << "Product 2: " << prod2 << endl;
cout << "Product 3: " << prod3 << endl;
cout << "Product 4: " << prod4 << endl;
cout << "Product 5: " << prod5 << endl;

Wait(); // Creates a "Press Enter to continue" statement
}

/* This function determines whether the input product number is
correct. The value must be an integer between 1 and 5. If it is
valid, then a value of 0 is returned. If it is invalid, then a
value of 1 is returned.*/

When something can have two values meaning 'ok' or 'bad', use a bool
which can have the values 'true' or 'false' :

bool b = true;
b = false;
int Check_Number(int prodnum)
{
int return_this;

switch (prodnum)
{
case 1:
case 2:
case 3:
case 4:
case 5:

indicate intentional fall-through with a comment since in some cases,
it may not be clear if it is or not.
return_this = 0;
break;
default:
return_this = 1;
break;
}
return return_this;

What about something like

return (prodnum >= 1) && (prodnum <= 5);

assuming the function returns a bool.
}


/* This function will use a string to create a pause until the user
hits the enter key. Any other characters entered will be ignored.*/

void Wait()
{
string response;
cout << "Press Enter to continue";
getline(cin, response);
}

/* This function will have the number of products passed into it.
From this data, it will determine the amount of each product sold
and total gross sales for the week. Then, it will display the data.*/

void Final_Data(int prod1,int prod2,int prod3,int prod4,int prod5)
{
double p1total, p2total, p3total, p4total, p5total, gross;

p1total = (prod1 * 2.98);
p2total = (prod2 * 4.50);
p3total = (prod3 * 9.98);
p4total = (prod4 * 4.49);
p5total = (prod5 * 6.87);
gross = (p1total + p2total + p3total + p4total + p5total);

system ("cls"); // Clear Screen

cout << "\n\n\nYour final sales report for this week is as follows:";

cout << "\n\nProduct Number\tAmount Sold\tTotal Income";
cout << "\n1\t\t" << prod1 << "\t\t" << "$" << p1total;
cout << "\n2\t\t" << prod2 << "\t\t" << "$" << p2total;
cout << "\n3\t\t" << prod3 << "\t\t" << "$" << p3total;
cout << "\n4\t\t" << prod4 << "\t\t" << "$" << p4total;
cout << "\n5\t\t" << prod5 << "\t\t" << "$" << p5total;

cout << "\n\nYour total gross sales for this week is $" << gross
<< "\n\n\n";
}


Jonathan
 
J

Jonathan Mcdougall

cout << "\n\n\nSelection: ";
Eh?

-Mike

Mmm.. I though \n was not portable, I should have informed myself.
Let me rephrase.

I prefer using std::endl since it is clearer than a bunch of \n.
It is also supposed to flush the buffer, but flushing the buffer
does not necessrily mean that the data is written.


Jonathan
 
D

Dick Bridges

Kevin Goodsell said:
Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

-Kevin

What's the "approved" method of marking the text you're responding to when
the corporate-mandated, braindead reader (initials are LN) doesn't do it for
you. I've been using <previous></previous> but that seem hokey.
 
R

Ron Natalie

What's the "approved" method of marking the text you're responding to when
the corporate-mandated, braindead reader (initials are LN) doesn't do it for
you. I've been using <previous></previous> but that seem hokey.

When you press shift and the period key at the same time it prints a >
character. Do that on each included line. This will also encourage
deleting unnecessary quoted material (less >'s to manually insert).
 
D

da Vinci

Don't! This code is very hard to read. Do not use tabs since some
newsreaders (as mine) remove them, but at least indent by one or
two spaces.

OK. Sorry about that. When I copied it into the newsreader program to
post it, it looked very unorginized as the text wraps at a certain
point. I will keep this in mind next time.
The program in general is quite cool. A couple of guidelines though :

Thanks. I've been programming C++ since August and this is the most
complicated I've done yet. I really love this stuff. My only
background is BASIC on a Commodore 64 and Turbo Pascal on a 386. So I
am REALLY liking C++! By the way, the requirement for this program was
to use a single switch. As I said, above and beyond!
1) give better names to your variables and functions

One problem I have in doing so is that the teacher has an issue with
her compiler and lines being longer that 70 characters. So, we are
limited in how long we can have a single line. If you notice, I had to
break a bunch of my "long" output lines in half because of this. When
I first designed the program, wrote the pseudocode, and started
coding, I had long variables with _ in them that were specific.
Unfortunantly, I had gone over the 70 characters on a lot of lines.
So, I ended up nixing parts of the variables (product turned to prod)
ect.
2) try to break your code a bit more; a function must have a
representative name. If the name is too general (such as do_this() ),
break it until its name represents exactly what it does.

OK, I will keep this in mind. After reading your full post, I am going
to make one or two more functions.
3) too much comment is like not enough

I hate it when things walk a fine line between good and bad! When I
first started, I did not like using comments. Mainly because these
programs are so small and simple. I know they would be needed if I was
working within a group. So I try to make sure to comment a lot or else
I wouldnt put any in! Maybe I am over doing it? :)
4) learn about arrays and vectors

Very soon! I actually want to use an array for the character input at
certain places. This program will crash if someone enters two or more
characters when it is specifically looking for a Y or N. If the user
mis-keyed and hit NB or something (I did once and that is how I found
the problem) it will lock up the program. If I used an array, then
just looked at the [1] position, if they entered NB, it would only see
the N. I think.... :)
5) don't give up!

NEVER! :)
<personal opinion>
take the habit of naming the variables even on declaration,
it makes things clearer for everybody
</personal opinion>

which variables? The ones that are being passed in or the ones that
will be used for the variables coming in?
take the habit of defining one variable per line and
to comment each of them. "various program functions" is not
very clear imo :)

Does it matter how "long" the code is? I know when it is compiled, it
will be the same size regardless of how much white space you use in
the source. There are no draw backs or professional "no-no's" about
doing things line by line instead of grouping?
Display_Main_Menu has a misleading name. It does not
only display, it asks for an option and returns it. Either
rename your function or (preferrably) break it in two.

Very good point. Thank you!
non portable, be careful. A system without 'cls' will do
nothing. On my system, 'cls' from the command prompt formats
the disk without warning. its a good thing I did not run it :)

Is there a portable way to clear a screen without using a mass of endl
or \n's?
look out std::toupper()

I've used the toupper() a few times in other program. I don't know why
I didn't use it here. Thanks for the reminder.
mmm.. what about an array or a std::vector here (or even a std::map)?
If you didn't learn about them, that's ok, but know that it would
be a lot simpler.

Your speaking Greek again. :) I am vaguely familiar with Arrays and
plan to tackle them next.
Every case should look like these 3 : calling a function or two,
but that's it. If not, well.. it makes it harder to understand.

Very true. The problem I ran into when thinking of what I could put
into functions is that I do not know how to do reference or pointers
yet. According to the professor, without one of those I can only pass
one value out of a function.
When something can have two values meaning 'ok' or 'bad', use a bool
which can have the values 'true' or 'false' :

bool b = true;
b = false;

So would this be good to use as a continue type statement?

bool continue;
continue = false;

while (continue == false) {
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if (continue == 'Y')
continue = true;
else
continue = false;
}

indicate intentional fall-through with a comment since in some cases,
it may not be clear if it is or not.

Excellent advice.
What about something like

return (prodnum >= 1) && (prodnum <= 5);

assuming the function returns a bool.

Interesting. I had't thought of that (obviously!). I will give it a
shot.

Thanks for the ideas and help. This is obviously a tight-knit
community and glad to be apart of it.
 
J

Jonathan Mcdougall

Please don't top-post. Read section 5 of the FAQ for posting guidelines.
What's the "approved" method of marking the text you're responding to when
the corporate-mandated, braindead reader (initials are LN) doesn't do it for
you. I've been using <previous></previous> but that seem hokey.

There are none, but people use
|
:

or whatever. Just try to make it clear about who you are responding
to and quote only what you are answering to. Do not mess the quotes
either.


Jonathan
 
J

Jonathan Mcdougall

3) too much comment is like not enough
I hate it when things walk a fine line between good and bad! When I
first started, I did not like using comments. Mainly because these
programs are so small and simple. I know they would be needed if I was
working within a group. So I try to make sure to comment a lot or else
I wouldnt put any in! Maybe I am over doing it? :)


Yeah, things like

while ( good_or_bad != 0 )
{
cout << "\n\n\nEnter product number (1-5): ";
cin >> prod_num;
good_or_bad = Check_Number(prod_num);
} // Ends Inner While

You know, it is _clear_ that the bracket ends this while. Or better :


4) learn about arrays and vectors

Very soon! I actually want to use an array for the character input at
certain places. This program will crash if someone enters two or more
characters when it is specifically looking for a Y or N. If the user
mis-keyed and hit NB or something (I did once and that is how I found
the problem) it will lock up the program. If I used an array, then
just looked at the [1] position, if they entered NB, it would only see
the N. I think.... :)
5) don't give up!

NEVER! :)
<personal opinion>
take the habit of naming the variables even on declaration,
it makes things clearer for everybody
</personal opinion>

which variables? The ones that are being passed in or the ones that
will be used for the variables coming in?
take the habit of defining one variable per line and
to comment each of them. "various program functions" is not
very clear imo :)

Does it matter how "long" the code is? I know when it is compiled, it
will be the same size regardless of how much white space you use in
the source. There are no draw backs or professional "no-no's" about
doing things line by line instead of grouping?
Display_Main_Menu has a misleading name. It does not
only display, it asks for an option and returns it. Either
rename your function or (preferrably) break it in two.

Very good point. Thank you!
non portable, be careful. A system without 'cls' will do
nothing. On my system, 'cls' from the command prompt formats
the disk without warning. its a good thing I did not run it :)

Is there a portable way to clear a screen without using a mass of endl
or \n's?
look out std::toupper()

I've used the toupper() a few times in other program. I don't know why
I didn't use it here. Thanks for the reminder.
mmm.. what about an array or a std::vector here (or even a std::map)?
If you didn't learn about them, that's ok, but know that it would
be a lot simpler.

Your speaking Greek again. :) I am vaguely familiar with Arrays and
plan to tackle them next.
Every case should look like these 3 : calling a function or two,
but that's it. If not, well.. it makes it harder to understand.

Very true. The problem I ran into when thinking of what I could put
into functions is that I do not know how to do reference or pointers
yet. According to the professor, without one of those I can only pass
one value out of a function.
When something can have two values meaning 'ok' or 'bad', use a bool
which can have the values 'true' or 'false' :

bool b = true;
b = false;

So would this be good to use as a continue type statement?

bool continue;
continue = false;

while (continue == false) {
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if (continue == 'Y')
continue = true;
else
continue = false;
}

indicate intentional fall-through with a comment since in some cases,
it may not be clear if it is or not.

Excellent advice.
What about something like

return (prodnum >= 1) && (prodnum <= 5);

assuming the function returns a bool.

Interesting. I had't thought of that (obviously!). I will give it a
shot.

Thanks for the ideas and help. This is obviously a tight-knit
community and glad to be apart of it.
 
J

Jonathan Mcdougall

3) too much comment is like not enough
I hate it when things walk a fine line between good and bad! When I
first started, I did not like using comments. Mainly because these
programs are so small and simple. I know they would be needed if I was
working within a group. So I try to make sure to comment a lot or else
I wouldnt put any in! Maybe I am over doing it? :)


Yeah, things like

while ( good_or_bad != 0 )
{
cout << "\n\n\nEnter product number (1-5): ";
cin >> prod_num;
good_or_bad = Check_Number(prod_num);
} // Ends Inner While

You know, it is _clear_ that the bracket ends this while. Or better :

return 0;
} // Ends Main

This was actually funny :)
Very soon!

Be careful with them though. I suggest you take a look to std::vector
before arrays, since arrays are error-prone, but I suppose your
teacher knows what she's doing.
which variables? The ones that are being passed in or the ones that
will be used for the variables coming in?

Both (if I understand correctly) :

// variables named
void f(int a, int b);

int main()
{
f(1, 2);
}

// ditto
void f(int a, int b)
{

}
Does it matter how "long" the code is? I know when it is compiled, it
will be the same size regardless of how much white space you use in
the source. There are no draw backs or professional "no-no's" about
doing things line by line instead of grouping?

No. Depending on the company standards, you will do things differently,
but for programs like that, try to make it as clear as possible.

Is there a portable way to clear a screen without using a mass of endl
or \n's?

No. C++ can be used in toasters, you know.
Your speaking Greek again. :) I am vaguely familiar with Arrays and
plan to tackle them next.

Mmm... if you have a choice, forget arrays right now.

http://cplus.about.com/library/weekly/aa050102e.htm

Or have a Google on std::vector. Do not stop on details like
the weird std:: notation. Try to understand the whole.

Post questions here if you don't understand.
Very true. The problem I ran into when thinking of what I could put
into functions is that I do not know how to do reference or pointers
yet. According to the professor, without one of those I can only pass
one value out of a function.

It does not matter whether you pass by reference or by value. But
actually, you are right. It would be a bit more complicated
to seperate that one.

What would be interesting is to keep that program and to work on
it as your knowledge increases, tweaking it and making it look
right.
So would this be good to use as a continue type statement?

bool continue;
continue = false;

while (continue == false) {
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if (continue == 'Y')
continue = true;
else
continue = false;
}

Could be, except that you use 'continue' as a char and a boolean,
but you understand. Personaly, something like

do
{
cout << "Do you want to continue(Y/N)?";
cin >> continue;

} while ( std::toupper(continue) != 'Y' );

If you are not familiar with this version of while, you can
stay with the preceeding one, it is quite good. Or try this

while ( true ) // infinit loop
{
cout << "Do you want to continue(Y/N)?";
cin >> continue;

if ( std::toupper(continue) == 'Y' )
break; // exits the loop
}

which gets rid of a variable.
Thanks for the ideas and help. This is obviously a tight-knit
community and glad to be apart of it.

Hehe.. depends on the people.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top