Thread: Problems with Looping

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    25

    Problems with Looping

    I have a c++ problem regarding looping. I am writing a program which will include a problem similar to below.

    1) ok, i am writing a program that asks the user to enter a number.

    2) Then ask the user if the number entered was correct.

    3) If the number is correct, the user will enter 'y' or 'yes' and the program will terminate.
    (pretty simple so far ?

    4) If the user enters 'n' or 'no' the program should loop back to the beginning of the program and start again. goes back to 1)

    5) However, if the user enters anything other than 'yes' or 'no', the program rejects this input and tells the user to re-enter, using only 'yes' or 'no'.

    6) This should be asked infinitely until either 'yes' or 'no' is inputted, after which the program then loops back to the beginning.
    how would i do this ?

    i've been trying do while and switch but no luck. would i have to use a for loop ?

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If you've been trying to do this "for a while, " then you've got some code that shows that and some errors and/or problems you'd like to address. Let's see both/all of those.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by rags_to_riches View Post
    If you've been trying to do this "for a while, " then you've got some code that shows that and some errors and/or problems you'd like to address. Let's see both/all of those.
    Code:
    #include <iostream.h>
    #include <string>
    #include <conio.h>
    
    main()
    {
    	string date;
       char answer; //either yes or no
       bool check;
    
    do	{
    		cout << "\nplease enter the date in dd/mm/yyyy: "; //beginning of loop********************************
          cin >> date;
    
          cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n";
          cin >> answer;
    
          switch (answer) {
          	case 'y':
             	cout << "\nThank you!!\n";
                break;
    
          }while (answer != 'y');
       }  getch();
    }
    ok then, if u insist.
    here we go then.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There isn't such a thing as a switch-while loop. Perhaps you want a switch inside of a loop.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by tabstop View Post
    There isn't such a thing as a switch-while loop. Perhaps you want a switch inside of a loop.
    how would you go about writing what i am trying to do ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And now upon rereading I see that very lonely do up there at the top. So the loop appears to be fine -- the question now is: what do you want to do? Do you only want to give a message when they type 'y'? What if they type 'n'? What if they type 'q'? The requirements state you need yet another loop to handle the case of weird input.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    There's no closing '}' on the switch block.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    You could use a while loop and get rid of the switch, just use an if statement.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    BTW my compiler does not like <iostream.h>

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Code:
    #include <iostream.h>
    #include <string>
    #include <conio.h>
    
    main()
    {
    	string date;
       char answer; //either yes or no
       bool check;
    
    do	{  //beginning of loop********************************
    		cout << "\nplease enter the date in dd/mm/yyyy: ";
          cin >> date;
    
          cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n";
          cin >> answer;
    
          switch (answer) {
          	case 'y':
             	cout << "\nThank you!!\n";
                break;
    
          }
    	}while (answer != 'y');
    }
    here is what i ended up with

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    i'm sure an if statement wouldnt loop though

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    The if would replace the switch. Inside the loop.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Does your program work now?

  14. #14
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by fadlan12 View Post
    Does your program work now?

    you mean,

    Code:
    #include <iostream.h>
    #include <string>
    #include <conio.h>
    
    main()
    {
    	string date;
       char answer; //either yes or no
       bool check;
    
    do	{  //beginning of loop********************************
    		cout << "\nplease enter the date in dd/mm/yyyy: ";
          cin >> date;
    
          cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n";
          cin >> answer;
    
          if (answer = 'y')
          cout << "\nThank you!!\n";
          break;
    
    
    	}while (answer != 'y');
    }
    it wont loop with the if

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Can you get it to compile without my suggestion? It won't compile for me at all with your code. I got it to work with a while loop and an if statement inside. But your code is missing a few things, for ex when you compile it says string, cin, cout etc not declared.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with nesting in looping
    By wobbles in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2010, 02:30 AM
  2. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  3. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  4. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM