I'm in an Intro to programming C++ class. We are just getting to using switches. Which i'm not having a problem with. I'd just like to know how to get a program to back to the start point at the end. We might have made it this far yet, but I just have to know. The whole press any key to continue...and having my program close it just so ugly to me. I'll include code for a program we just had to write. Any help would greatly appreciated....I just gotta know how to do this.
And if you still need help with the program Just speak up and I will give you an example of what you have to do... but I would read about looping and try it yourself first..
deviantnature, the teacher will probably say it's balderdash and that it doesn't matter. As far as I know, system("pause"); doesn't work on a standard *nix system which means you just removed two mainstream operating systems accessibility from your program.
The reason why goto statements are bad is because they have a (extremely) large tendency to reduce code readability and lead to bad program structure, which is by all means, frustrating.
Also, for a more stable way to receive numbers from console, I suggest you read this: http://cplusplus.com/forum/articles/6046/.
alrighty....well while i'm at it then. what is the best way to pause my program instead of using system("pause"). I see a ton of posts on why it's bad...
Commonly, a cheap yet better way to go about it is to use std::cin.get();. This is a blocking function that waits for the users input and returns the input he gives as character casted to an integer which can but doesn't need to be used.
I've tried to insert that into my code inplace of system("pause")....but its not working it just closes right away still. i must be using it wrong so here it with that change.
That method noted by computerquip is the best way to go short of API means which are generally more complicated. It also does not face the problems of leftover newlines.
.get() will also work, but it accepts newlines. Whenever you use cin, you press enter afterwards to confirm input. That newline is left in the input buffer. On your next cin, it clears all leading whitespace (the newline) and then after you send in the input you leave a fresh newline in its place, etc. That leaves a newline at the very end of the buffer for get() to accept. (cin does not accept plain newlines.) Therefore you .ignore() the newlines out of the buffer. But you can just perform that ignore call instead and it still works.
Whenever you get input from the user, remember that
the user will always press ENTER after every input
Hence, every time you get input from the user, you should get rid of the ENTER key (newline). It is common enough of a problem that it is worth just making a little procedure for it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <limits>
template <typename T>
std::istream getuserinput( std::istream& ins, T& item )
{
// Read the desired item, if possible
ins >> item;
// Get rid of the newline (and any extraneous trailing characters)
// without messing up the current state flags.
std::ios_base::iostate flags = ins.rdstate();
ins.clear();
ins.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
ins.setstate( flags );
// Just like getline(), return the input stream
return ins;
}
Now you can use it everywhere you would use cin >> x.
1 2 3 4 5 6 7 8 9 10
string name;
int age;
cout << "What is your name? " << flush;
getline( cin, name );
cout << "How old are you, " << name << "? " << flush;
getuserinput( cin, age );
cout << "Name: " << name << "\nAge: " << age << "\n";
Error checking against bad input should still be done, as usual:
8 9 10 11 12 13 14 15 16 17
getuserinput( cin, age );
if (!cin)
{
cout << "Hey, that's not an integer number!\n";
cin.clear();
}
if (age < 0)
{
cout << "What? I don't believe you!\n";
}
Variations on how you handle errors are up to you, of course.
Likewise, if you are only getting integers, you can skip the template stuff and have a procedure that only gets integers.
In all cases, however, you now have a little procedure that gets input from the user and gets rid of the newline (ENTER key) that he typed.