some help with loops is needed..

Could you please help wit this problem??
I do not understand why my "exit" loop is ignored!
the prgram runs fine but if I enter "exit" it doesn't exit!!

Any idea on how to fix this?

(The program simply turns spelled-out numbers ("one, "two"..) into numbers (1,2..)

const string exit="exit";
string wrd="";   

while (wrd!=exit)           //CHECK IF THE STRING IS "exit" (ignored!!!)
{
 int a=0; 
 
 vector<string>nmbs(10);      //just the vector
 nmbs[0]="zero";
 nmbs[1]="one";
 nmbs[2]="two";
 nmbs[3]="three";
 nmbs[4]="four";
 nmbs[5]="five";
 nmbs[6]="six";
 nmbs[7]="seven";
 nmbs[8]="eight";
 nmbs[9]="nine";

 cout<<"\nenter an integer between 1 and 10\n\n";

 while (cin>>wrd)                                //take the string
 
   {cout<<"\n";
       for (int i=0;i<nmbs.size();i++)
       
       {if (wrd==nmbs[i]) {cout<<i; break;}           //see if the string is fine
        else if (wrd!=nmbs[i]) a++;
                             if (a!=nmbs.size()-1);
                             else cout<<"wrong input??";}        //if it's not fine then say "wrong.."
                             a=0;
    cout<<"\n\n";}
 
}
what kind of compiler are you using?
Dev C++

I couldn't download "Visual Studio Free Ediion|"
This is not a compiler issue this is a programmer issue (hint : it's rarely if ever the compilers fault)

I'm being nitpicky here but you do not need to do
string wrd="";

the <string> library automatically initializes a string to "" in the default constructor. (see : http://www.cplusplus.com/reference/string/string/string/ )

[edit: in case that went over your head, just use the code string wrd; there is no need to re-initialize[read: re-set] the value to "" [or empty])

Before I go any further though, clodi I want you to indent your code so it's easier to read(for your sake and ours)

Doing things like : {if (wrd==nmbs[i]) {cout<<i; break;} obscures in which block a certain line of code lies, there is no point in saving space if you can't get your program to run.

Go through your program but use a pen and paper, follow the logic and you will see where your error is.

One last thing : if (a!=nmbs.size()-1); if a is not equal to the vector size then what? If you run through it with a pen and paper it will help clarify where you messed up. If you are still struggling after you've attempted it feel free to reply and i'll give you the solution.
Last edited on
Thank you so much.. I start rght now
Wow. it works now!!
well,
if (a!=nmbs.size()-1);
then do nothing! that's what I meant anyway.. is it wrong?

Also, is there a way I can avoid that embarassing "else if else if else if.." for each letter? how can I make C++ understand this check:

string "1" equal to int 1


I think my code looks silly otherwise..

const string exit="exit";
string wrd="";   

 int a=0; 
 
 vector<string>nmbs(10);
 nmbs[0]="zero";
 nmbs[1]="one";
 nmbs[2]="two";
 nmbs[3]="three";
 nmbs[4]="four";
 nmbs[5]="five";
 nmbs[6]="six";
 nmbs[7]="seven";
 nmbs[8]="eight";
 nmbs[9]="nine";

 cout<<"\nenter an integer between 1 and 10\n\n";
 cout<<"<exit> to leave the program\n\n";

 while (cin>>wrd and wrd!=exit)
 
   {cout<<"\n";
            for (int i=0;i<nmbs.size();i++)
       
            {if (wrd==nmbs[i])

                             {cout<<i; break;}

            else if (wrd!=nmbs[i])
                             
                             a++;
                             if (a!=nmbs.size());
                             else if (wrd=="0") cout<<"zero";
                             else if (wrd=="1") cout<<"one";
                             else if (wrd=="2") cout<<"two";
                             else if (wrd=="3") cout<<"three";
                             else if (wrd=="4") cout<<"four";
                             else if (wrd=="5") cout<<"five";
                             else if (wrd=="6") cout<<"six";
                             else if (wrd=="7") cout<<"seven";
                             else if (wrd=="8") cout<<"eight";
                             else if (wrd=="9") cout<<"nine";
                             else cout<<"wrong input??";
              }
              a=0;
        
    cout<<"\n\n";}

cout<<"\nbye bye!! (any key to exit)\n\n";
wrong? sort of, it's not efficient. You will see the check is not needed at all

If you do
if(a = nmb.size()-1) cout << "Wrong input";
you will avoid the unnecessary check. Why? Because now if (a!=nmb.size()-1) nothing happens at all, since we don't have any code for it.

There are ways around the if elses such as switch statements but you will be using if elses frequently in your programming adventures, regardless of which field you concentrate in.

For information regarding conversions between strings and ints ( they are two different data types ) see : http://www.cplusplus.com/forum/articles/9645/
Last edited on
thanks a lot.
I've got still a lot to learn I guess :)
Topic archived. No new replies allowed.