I am writing a application for Arch Linux, actually I already wrote it, but I wanted to do it a little differently then the other. The origional one uses the goto statement. Here is the code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
   cout
    << "\tAurDown v2.1\n"

    << "\tLicense: GPL3\n\n"
    << "Menu\n"
    << "1. Search for package\n"
    << "2. Download a package\n";
   cout << "Enter Selection\t";
   cin >> nSel;
 
  while((nSel != 1) || (nSel != 2))
    {
   cout << "Please Enter a valid selection"\n;
   cin >> nSel;
         
    }
}

I am wanting to it to print "Please enter a valid selection" as many times as somebody enters anything other than 1 or 2 and if the person enters 1 or two after they get the error "Please enter a valid...." it will resume. Sorry this is a newbie question. I know it has to be a simple problem, but can't find it.

Thanks

First of all you havent declared nSel any where but you are using it, so put int nSel = 0; before the cout lines or something (just before usage).

Then for your while loop you should make it so it loops while nSel is not 1 AND 2 not or. while((nSel != 1) && (nSel != 2))

commented: Helped me with my code issue +1

Ah thanks so much

You can use cases..

#include <iostream>
#include <string>
using namespace std;

char nSel;
void getinput();

int main()
{
   cout
    << "\tAurDown v2.1\n"

    << "\tLicense: GPL3\n\n"
    << "Menu\n"
    << "1. Search for package\n"
    << "2. Download a package\n";
   cout << "Enter Selection\t";
   getinput();
return 0;
}


void getinput()
{
     cin>>nSel;
     cin.ignore();
     switch(nSel)
     {
              case '1': cout<<"You chose to Search for a package\n";  //U can put anything here...
                        break;
              case '2': cout<<"You chose to download a package\n";
              break;
              default: cout<<"Please Enter a valid selection\n";
                       getinput();
              break;
     }     
}

That should definitely work.. If the user presses

1.. it prints they selected to search.
2.. It prints the selected to download..

Anything else, It prints "Please Enter a valid selection" and lets them choose again.. if they enter anything else again, it just repeats until they enter a valid selection such as 1 or 2.

The function "getinput();" is a prototype function.. But prototype functions work wonders.. especially in situations like these!

The function "getinput();" is a prototype function..

I've never heard the term used like that. What exactly does it imply?

Also, what could be considered an opinion or design choice, but the do/while loop avoids the potential stack issues with the recursive function.

Maybe he's thinking Java

The function "getinput();" is a prototype function.. But prototype functions work wonders.. especially in situations like these!

I'm guessing that triumphost meant to say function prototype

That's what I was thinking, too, but it doesn't make sense in the context. LOL I guess we'll have to wait and see what he says.

yes I meant function prototype..

That is how you use it :S
I've used that code and it compiled perfectly. No ancient.. I meant c++
See the managed attachments for examples of the code in action..
Written by myself of course but yeah I meant function prototypes.. for some reason I always call it prototype function :S

I don't quite understand how it "worked wonders" then. Unless you've got your function definitions out ahead of main(), there should always be prototypes. Anyway, thanks for clarifying it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.