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))

Comments
Helped me with my code issue

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!

Edited 5 Years Ago by triumphost: n/a

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.

Edited 5 Years Ago by jonsca: n/a

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

Edited 5 Years Ago by triumphost: n/a

Attachments
//PatcherScript
#include <iostream>
#include <windows.h>

using namespace std;

char x,y,z;

void RegRun();
void saymsg();
void getinput();
void askagain();
void start();       

int main()
{
    /*system("\"C:/Program Files/Alwil Software/Avast5/AvastUI.exe\"");*/        //Commented out for compatibility reasons; Added to Reg Key.
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
    cout<<"Please Install Avast Included with this Program!\n";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
	cout<<"If already installed ignore the message above and continue..\n";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
	system("PAUSE");
	cout<<"\n";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
    cout<<"System Registry must be Edited Once more to remove the Last Key!\n";
    cout<<"And add a New one which will be deleted later\n\n";
    start();
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    cout<<"Editing Finished According To User:\n";
    system("shutdown -r");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    cout<<"Restarting Computer in 60 Seconds\n\n";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
    cout<<"PRESS F8 REPEATEDLY WHEN THE COMPTUER RESTARTS TO GET INTO SAFEMODE!!\n\n";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    cout<<"SAFEMODE IS A MUST!! FOR YOUR OWN SYSTEM'S PROTECTION!\n";
    system("PAUSE");
    
    return 0;
}


     //Functions(1) Defined After Main and Before Main as Prototypes... Taken from old project "test cases.cpp" & Modified to Suit...


                                             
void RegRun()
{
     system("C:/Windows/RunOnce2.reg");
}

void saymsg()
{
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
     cout<<"Did you press yes to edit the registry this time?\n";
}

void getinput()
{
     cin>>x;
     cin.ignore();
     switch(x)
     {
              case '1': false;
                        break;
              case '2': askagain();
              break;
              default: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                       cout<<"You entered A letter or character that is not in the menu\n";
                       cout<<"Please try again!\n";
                       SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
                       cout<<"Did you press yes to allow registry editing?  y = 1  n = 2\n";
                       getinput();
              }     
}
void askagain()
{
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
     cout<<"Would you like to try editing the registry again?? 1 = Yes , 2 = No just exit!\n";
                   cin>>y;
                   cin.ignore();
                   switch(y){
                             case '1': 
                                  RegRun();
                                  saymsg();
                                  getinput();
                                  break;
                             case '2': exit(0);      // want to make the program terminate... tried return 0 but it failed...
                             break;
                             default: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                                      cout<<"You entered A letter or character that is not in the menu\n";
                                      cout<<"Please try again!\n";
                                      askagain();
                             break;
                             }
                   }
                   
void start()
{
     RegRun();
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
     cout<<"Did you press yes to allow registry editing?   y = 1   n = 2\n";
     getinput();
}
//PatcherScript2
#define _WIN32_IE		0x0500
#include <windows.h>
#include <lm.h>
#include <assert.h>
#include <iostream>
#include <winuser.h>
#include <stdlib.h>
#include <string>
#include <stdio.h>
#include <fstream>
#include <algorithm>
#include <shlobj.h>
#include <direct.h>
#include <windows.h>
#include <Shellapi.h>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <winable.h>
#pragma comment(lib, "Netapi32.lib")

using namespace std;

char x;
void continueprog();
void remove();
void movefile();

int main()
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
    cout<<"IF U ARE NOT IN SAFEMODE, EXIT THIS PROGRAM NOW!! AND DO IT!\n"; 
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); 
    system("PAUSE");
    cout<<"\n";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
    cout<<"Checking if Processes were killed!\n";
    system("taskkill /F /T /IM AvastUI.exe");
    cout<<"\n\n";
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    cout<<"NOTE: IF Process is NOT FOUND, CONTINUE! If Process Cannot be TERMINATED,\n";
    cout<<"You must RETRY stopping all services!\n\n";
    continueprog();
    
    
    BlockInput(true);
    Sleep(5000);
    BlockInput(false);
    
    Sleep(5000);
}

void continueprog()
{
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
     cout<<"Continue = 1  ,  Retry = 2\n";
     cin>>x;
     cin.ignore();
     switch(x)
     {
              case '1': cout<<"Continuing\n";
                        remove();
                        movefile();
                        break;
              case '2': cout<<"Retrying.. Please Follow the Instructions on Screen\n";
                        main();
                        break;
              default:  cout<<"You chose an option/character that is not on the menu, retry\n\n";
                        continueprog();
                        break;
                        }
}


void remove()
{
     if( remove( "C:\\Program Files\\Alwil Software\\Avast5\\ashBase.dll" ) != 0 ){
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
     perror( "Error deleting Old Ashbase.dll\n" );
     }
     else{
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
     puts( "Old Ashbase.dll successfully deleted\n" );
     }
}

void movefile()
{
     if ( rename("C:\\Windows\\ashBase.dll", "C:\\Program Files\\Alwil Software\\Avast5\\ashBase.dll") != 0){
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
     perror( NULL );
     getchar();
     }
     else{
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
     puts( "Ashbase.dll Replaced Successfully\n" );
     Sleep(5000);
     }
     exit(0);
}

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.

Edited 5 Years Ago by jonsca: n/a

This article has been dead for over six months. Start a new discussion instead.