how do you repeat cout statements?

This is a discussion on how do you repeat cout statements? within the C++ Programming forums, part of the General Programming Boards category; ok my question is if a person in used my code and pressed 0 to not roll how do you ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    34

    how do you repeat cout statements?

    ok my question is if a person in used my code and pressed 0 to not roll how do you repeat it again until the user enters 1 for yes?


    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <cmath>
    // this one is for the system library =)
    #include <cstdlib>
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int num1,num2;
    int yes;
    int main ()
    {
    
      cout << "Chapter Program 5\n";
      cout << "Round 1\n";
      cout << "Would you like to roll this round? [1 for yes, 0 for no]:";
      cin  >> yes;
    if (yes < 1)
      cout << "Would you like to roll this round? [1 for yes, 0 for no]:";
      
    else
    cout << "You have rolled a" << num1 << "and a" <<num2 << endl;
    
    {
    
    }
        system("pause\n\n\n\n");
    return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
       for ( ;; )
       {
          int yes;
          cout << "Would you like to roll this round? [1 for yes, 0 for no]:";
          if ( cin >> yes && yes == 1 )
          {
             cout << "M'Kay!\n";
          }
          else
          {
             break;
          }
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    	int num = 0;
    	
    	do
    	{
    		cout<<"Enter a number: ";
    		cin>>num;
    	
    	}while(num != 20);
    
    
    	num = 30;
    	while(num < 50)
    	{
    		cout<<"Enter another number: ";
    		cin>>num;
    	}
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    i get a problem that when you enter 0 you get 2 lines saying the same thing. how do i get rid of one of the lines?



    Code:
    //adds the iostream library to the program
    #include <iostream>
    //adds the iomanip library to the program
    #include <iomanip>
    //adds the math library to the program
    #include <cmath>
    // this one is for the system library =)
    #include <cstdlib>
    
     
    //informs the compiler you are using the standard library set
    using namespace std;
    
    int num1,num2;
    int Choice;
    int main ()
    {
       for ( ;; )
       {
          int yes;
          cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
          if ( cin >> yes && yes == 0 )
          {
             cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
             cout << "\n\n\n";
          }
          else
          {
             cout << "you rolled\n\n\n";
             break;
          }
       }
           system("pause\n\n\n");
       return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    anyone got any answers to this annoying problem?

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    317
    i would do

    so you need it to look like this (below)
    Code:
    std::string yes;
    cin >> yes;
    if(yes == "yes"){do whatever you think of;}

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    where do you put that? in int main?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    any ideas?

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    using namespace std;
    
    int num1,num2;
    int Choice;
    int main ()
    {
       for ( ;; )
       {
          int yes;
          cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
          if ( cin >> yes && yes == 0 )
          {
             //cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";
             //cout << "\n\n\n";
          }
          else
          {
             cout << "you rolled\n\n\n";
             break;
          }
       }
           system("pause\n\n\n");
       return 0;
    }
    is this what you want ?
    Kurt

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    yes that is part of what i wanted to do but i need tomake it so that
    if a person enters anything else ( like A,0,2,3,5,35)but a 1
    then

    Code:
    //cout << "Would you like to roll this round? [1 for yes, 0 for no]:\n\n";

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    For your previous problem of the prompt appearing twice when they answer 0, you need to remove (or comment out as Enahs did) the extra prompt inside the if. This isn't necessary because if they answer 0, the loop wil continue again and the original prompt will be displayed.

    If you want to worry about bad input (something other than 0 or 1), then it is a little more complicated. A common way to handle bad input is this:
    Code:
    while (!(cin >> yes) || (yes != 0 && yes != 1))
    {
      // clear any fail state from the user entering characters instead of a number
      cin.clear();
    
      // remove all bad characters from the input stream to start fresh
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
      // Prompt again here...
    }
    
    // Use yes as you would normally.
    Note that you have to #include <limits> to use numeric_limits. You can also just use a large number there to make sure all bad characters are ignored.

    That code will keep prompting until 0 or 1 is input. Then you can handle 0 or 1 as you wish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 PM
  2. IF statements
    By Viperz0r in forum C++ Programming
    Replies: 11
    Last Post: 03-05-2006, 10:08 AM
  3. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 09:04 AM
  4. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  5. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21