while loop in C++

Posted on 2011-04-28
Last Modified: 2012-05-11
Hi, I am a beginner and I am working on a hw for C++.  My code is attached. I am confused about how to restart a loop (see first attached snippet).  I am supposed to run two tests on the user string.  One to test for quit and one to test to see if the user just hit enter.  My professor has provided these functions (see 2nd snippet) and I just call it twice (though not sure if I have called them correctly but that's another question) but with different parameters.

My question: how (after the calls to bool  test) do I go back to prompt the user again?  Obviously I could just enter the prompt again but that's not a good solution since user could just keep pressing enter.  

My first idea was to have a nested while loop testing for the two conditions but I'm pretty confused.
//main loop
        char user_input[40];
        char comm_portion[40];
        char para_portion[40];
        bool runProcesschar;

    while (1)

        printf("Enter string or type 'quit' to end: \n");


        //1st step - test for no text
        while ((bool test(user_input,"")==true))
          printf("Please enter string:\n");
        //2nd step - test for "quit" prompt
        if ((bool test(user_input,"quit")==true))
          return 0; //exits loop?

Open in new window

bool    test  (const char* first, const char* second) throw(const char*)


    if  ( (first == NULL)  ||  (second == NULL) )
        throw "NULL ptr to equals";

    return( strcmp(first,second) == 0 );

Open in new window

Question by:dazedandconfused69
    LVL 74

    Assisted Solution

    by:käµfm³d 👽

    while ((bool test(user_input,"")==true))

    Open in new window

    is invalid syntax. You don't put the return type in front of the call. You only enter the return type in the definition of the function.

    Your inner loop will never terminate because you don't have anything that actually changes the state of the machine at that point. You will repeatedly print "Error!\n" and "Please enter string:\n" because user_input doesn't change value inside the inner loop.

    As for "restarting" a loop, yes, you would have nested loops, most likely, a third loop would be needed.
    LVL 31

    Assisted Solution

    You didn't specify how getString(user_input) is defined. If you have to write it yourself, take a look at    istream& getline (char* s, streamsize n );

    You don't show how you are going to catch the exception. Here are some tutorials in catching exceptions:

    Does your assignment even require loops? If you are just expected to run two tests on the user string, then just call getString and test twice with no loops.
    LVL 16

    Accepted Solution

    Rather than placing the test() function calls inside of while loops, you could instead place them inside an if statement.  If the if is false, you can use a continue command to jump back up to the while(1).

    Here's an outline is pseudo code

      if( ! test(input) )
        printf("Error Msg");
        continue;   //jumps up to while(1) to display the prompt again
      if( test(input, quit) )
       return 0;  //exits subrotine
       //Or use break to exit while loop
        break;  //exits out of while(1) loop if return 0 above is removed
     ...more code here...
    //break; above would jump to here

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Title # Comments Views Activity
    java constructor error 8 63
    ClickOnce Install - Shortcut Question 3 40
    triangle challenge 4 36
    Unresolved External Symbols 3 32
    A short article about a problem I had getting the GPS LocationListener working.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now