Thread: how to make only letter r to restart?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    how to make only letter r to restart?

    hi i want the program below to restart only when specific letter is entered from keyboard....at this point the program accept any letter to restart except 'q'.

    so how im gonna do it will only except letter 'r' to restart? thanks.


    [tag]
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    
    
    using namespace std;
    
    int main (int argc, char *argv[])
    
    {
       char again, quit ;
       string name = "";
       while (quit != 'q')
            
            do{
                system ("cls");
                
                cout << "Hello, what is your name? \n";
                cin >> name;
     
                cout << "Hi " << name;
              
                
                cout << "\n Please press r to restart or q to quit and press enter";
                cin >> quit;
               
                  
            }               
                    
        
                   
            while(toupper(again) == 'r'); //restart program
    
    return 0;
    }
    [/tag]

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    For starters, if you're going to use toupper, why would you compare the result to a lower-case letter? And wouldn't you compare the user's input, rather than an uninitialized variable?

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    sorry i didnt get what u mean...my problem is when i press for e.g letter 'k' it will also restart the program.....i only want the letter 'r' to restart it.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We know what you want to do, and we know exactly why it doesn't work and how it should be done properly.

    The problem is that with your skill level you are unable to understand the responses given to you. The way to proceed forward is for you to ask questions about specific things in the responses given so far. For example if you don't know what an "uninitialised variable" is, then ask about that.

    quit is also uninitialised when it is first compared against 'q'.
    Probably your main mistake though, and it's really a design problem, is that you should only have one loop, not two.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Quote Originally Posted by imjustnew View Post
    hi i want the program below to restart only when specific letter is entered from keyboard....at this point the program accept any letter to restart except 'q'.

    so how im gonna do it will only except letter 'r' to restart? thanks.


    [tag]
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    
    
    using namespace std;
    
    int main (int argc, char *argv[])
    
    {
       char again, quit ;//quit:not assign value to variable?(Note:while loop)
       string name = "";
       while (quit != 'q')//Why?
            
            do{
                system ("cls");
                
                cout << "Hello, what is your name? \n";
                cin >> name;
     
                cout << "Hi " << name;
              
                
                cout << "\n Please press r to restart or q to quit and press enter";
                cin >> quit;
               
                  
            }               
                    
        
                   
            while(toupper(again) == 'r'); //restart program
    
    return 0;
    }
    [/tag]
    Error your basic.
    Sorry my english is very bad!.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    This seems to be a thread of two halves, there is another one that has been replied to more recently, regarding , quit rather than restart.

    Anyway, it is being shown to you that your 'quit' variable has to be initialised for the while loop.
    In any case you should think a little more descriptively about the variable names and use in the loop, just for clarity...
    that is why in the other post i used ('while goAgain is true'...(keep working..) ), rather than what you are saying here, 'while quit is not (q)uit'.

    And you do not need two loops in your present scenario, as has already been pointed out.
    Apart from a programming excercise, for me it is kind of redundant here to have stop/continue as two variables, just ask for input at the end of each loop and advise the user 'press (q) to exit or any other key to go again' and then test the input accordingly.
    Last edited by rogster001; 01-06-2012 at 03:02 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. make a char display a word or letter
    By dyelax in forum C++ Programming
    Replies: 12
    Last Post: 10-13-2009, 11:55 AM
  2. How To Make Capital Letter To Small Capital Letter??
    By jason07 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2005, 04:37 PM
  3. How can i make a "letter could not be found" break?
    By Welshy in forum C++ Programming
    Replies: 14
    Last Post: 04-12-2005, 02:41 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. Restart
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2002, 01:41 PM