Jump to content

newbie c++


Recommended Posts

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;

}

Link to comment
Share on other sites


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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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