[Help] Basic if user typed "x" do "y"

Posts 1–8 of 8 · Page 1 of 1
[Help] Basic if user typed "x" do "y"
Super c++ noob here, how do I make it so if the user types "end" the program will close and if they type something else it will: cout << "a";

This is everything ive found on the internet.

cout << "Type end to close" <<endl;
string type;
if(type == "end")
{
return (0);
}
else
cout << "a";
}
Code:
std::cin
cin
You basically want to get the user input:

Code:
cout << "Type \"end\" to close.";
char* userInput;
getline(cin, userInput); // changes 'userInput' value to what the user wrote

if (userInput = "end") {
// the user wants to end the program
} else {
// the user typed something else
}
reading a c++ book or taking a c++ class will help you with the fundamentals
Consider doing that before trying to write these programs
Quote Originally Posted by JunkZ View Post
Super c++ noob here, how do I make it so if the user types "end" the program will close and if they type something else it will: cout << "a";

This is everything ive found on the internet.

cout << "Type end to close" <<endl;
string type;
if(type == "end")
{
return (0);
}
else
cout << "a";
}
This would be the correct code (mpgh didn't let me tab...):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string>
using namespace std; //you won't have to std:: everytime.
int main()
{
string type; //inizialize type
cout<<"Type end to close"<<endl; //ask to cin
cin>>type; //cin
if(type=="end")
{
//do the stuff
}
else
{
//do other stuff
}
system("pause");  ( or getchar(); )
return 0;
}
a few general rules:
1) Inizialize everything at the beginning,not in the middle of your program.
2) The program must start at the beginning and finish at the end,if you make something like:
Code:
cin>>a;
if(a>0)
return 0;
else
cout<<"funneh";
return 0;
Then you gotta not use such thing.
While a computer will never say it's not fine,it creates alot of confusion and is globally avoided as much as possible.

A trick to fix this is to use a bool variable and setting it to true to any possible correct value,then the program will continue only if that variable is true,example:
Code:
//Program outputs "hello" only if a>5 and b<3
int a,b;
bool valuesarevalid=false;
cin>>a>>b;
if(a>5 && b<3)
valuesarevalid=true;
if(valuesarevalid==true)
cout<<"hello"<<endl;
system("pause");
return 0;
Quote Originally Posted by IV2B View Post
The program must start at the beginning and finish at the end,if you make something like:
Code:
cin>>a;
if(a>0)
return 0;
else
cout<<"funneh";
return 0;
Then you gotta not use such thing.
While a computer will never say it's not fine,it creates alot of confusion and is globally avoided as much as possible.

A trick to fix this is to use a bool variable and setting it to true to any possible correct value,then the program will continue only if that variable is true,example:
Horrible suggestion, you want code to return as soon as possible in the case of 'errors'... What would you do in case of null pointers in your scenario? flagging a bool to false and then using a null pointer for the rest of the code?

Your example code is horrible
Code:
int a, b;
std::cin >> a >> b;
if( a > 5 && b < 3 )
{
    std::cout<<"Hello"<<std::endl;
}
getch();
return 0;
Here it is just make main.cpp or whatever and paste this into it.

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string type;
	cout << "Type end to close" <<endl;
	cin >> type;
	if(type == "end")
		{
			return (0);
		}
	else
	cout << "a";

	cin.ignore();
	cin.get();
	return 0;
}
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us