[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

while loop in C++

Posted on 2011-04-28
3
Medium Priority
?
387 Views
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");

        getString(user_input);

        //1st step - test for no text
        while ((bool test(user_input,"")==true))
          {
          printf("Error!\n");
          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

0
Comment
Question by:dazedandconfused69
3 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 660 total points
ID: 35487906
This

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.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 660 total points
ID: 35488084
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 );
    http://www.cplusplus.com/reference/iostream/istream/getline/

You don't show how you are going to catch the exception. Here are some tutorials in catching exceptions:
     http://www.cprogramming.com/tutorial/exceptions.html
     http://www.cplusplus.com/doc/tutorial/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.
0
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 680 total points
ID: 35488315
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

while(1)
{
  printf(...);
  get(input);
  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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Starting up a Project

872 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