Jump to content

Recommended Posts

Posted

hi can you guys help me with my c++ im kinda newbie i dontknow why my program wont let me type in a response like yes and do what i want it to do and no so if you guys could fix it up for me please i would very much apprecieate it thanks heres the code

/*

My program about something just havent figured it out

yet

*/

#include <iostream>

#include <stdio.h>

using namespace std;

char c;

main()

{

cout << " Type in yes or no: \n";

repeat:

cin >> c;

if ( c = 'yes' )

{

cout << " Good Job!! :D !! ";

}

if( c = 'no' )

{

goto end;

}

else

cout << " Sorry That is not a correct response : \n";

goto repeat;

end:

return 0;

}


Posted

Wow, THIS really is code i will remember a long time :)

There are so many mistakes, i don´t know where to start, so i won´t start ;)

...label, goto, void main()...

Ok, so here comes a working one:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string strInput;
  bool bValidInput = false;

  cout << "Type in yes or no:" << endl;

  while (!bValidInput)
  {
     cin >> strInput;
     
     if ( strInput == "yes" )
     {
        cout << "You typed \"yes\"" << endl;
        bValidInput = true;
     }
     else if( strInput == "no" )
     {
        cout << "You typed \"no\"" << endl;
        bValidInput = true;
     }
     else
     {
        cout << "No correct input" << endl;
     }
  }

  return 0;
}

I know, it´s also not the best looking, but it think it´s ok for learning purposes.

The biggest advantage is, that it uses the "string" class to store and compare strings.

If you have more explicit questions, just ask.

so far.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...