just curious

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.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int main()
{
    int code = 0;
    
    cout << "Enter the code: ";
    cin >> code;
    
    switch (code)
    {
           case 1:
                    cout << "sales * .12"<<endl;
                    break;
           case 2:
                    cout << "sales * .12"<<endl;
                    break;
           case 3:
                    cout << "sales * .15"<<endl;
                    break;
           case 4:
                    cout << "sales *.20"<<endl;
                    break;
           default:
                   cout << "ERROR!" << endl;
    }//end switch
system("pause");
return(0);
}
You use a loop. That's the only answer. Calling main and using goto are not valid answers.
Here's the tutorial on loops, from this site:
http://www.cplusplus.com/doc/tutorial/control/
You really shouldn't use system( "pause" ); I hear nothing but bad things about it, here are some links you should check out about it.
http://www.cplusplus.com/forum/articles/11153/

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..
That's because it is bad. Avoid where possible. But I'd ignore it if it's just trivial programming stuff.
yea i had just read a few of those threads about the system("pause") stuff. Going to bring that up in my next class.
closed account (S6k9GNh0)
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/.
Last edited on
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...
closed account (S6k9GNh0)
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.
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//display the amount to multiply by sales.
#include <iostream>
using namespace std;

int main()
{
    int code;
    cout << "Enter the code: ";
    cin >> code;
    
    if (code == 1 || code == 2)
       cout << "Sales * .12" << endl;
    else
        if (code == 3)
           cout << "Sales * .15" << endl;
    else
        if (code == 4)
           cout << "Sales * .20" << endl;
    else
        cout << "ERROR!ERROR!ERROR!" << endl;
std::cin.get();
return(0);

}
What compiler are you using? and if you want to write your program with the switch statement you can also write it like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

int main()
{
    int code = 0;

    while ( code != 5 )
    {

        cout << "Enter the code(enter 5 to exit): ";
        cin >> code;

            switch (code)
            {
                   case 1:
                            cout << "sales * .12" << endl;
                            break;
                   case 2:
                            cout << "sales * .12" << endl;
                            break;
                   case 3:
                            cout << "sales * .15" << endl;
                            break;
                   case 4:
                            cout << "sales *.20" << endl;
                            break;
            }//end switch

            if ( code >= 6 )
            {
                cout << "Error" << endl;
                continue;
            }
    }

    return( 0 );
}


this is because when u call cin on line 9 it leaves the '\n ' in the input stream... u need to clear it manually by using cin.ignore();

@ony:
1
2
3
4
5
 if ( code >= 6 )
            {
                cout << "Error" << endl;
                continue;
            }


i would use the default: for it...
Last edited on
closed account (S6k9GNh0)
http://www.cplusplus.com/forum/articles/7312/

1
2
3
4
5
6
7
8
#include <iostream>
#include <limits>

void PressEnterToContinue()
{
   std::cout << "Press ENTER to continue... " << flush;
   std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
}
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.

Hope this helps.
Topic archived. No new replies allowed.