Help with declaring non numerical values in c++

Aug 20, 2014 at 10:34am
Write your question here.

Im a complete begginer in c++ so im having diffuculty with this problem.


This is my problem.

1. Status of boy ang girl should be less than single.

If i declare it like this


1
2
3
4
5
6
7
8
9
10
int main ()
{
int sg;
cin>>sg;

cout<<"Please enter relationship status:"<<sg<<;

If (!(sg==single))
    cout<<"Married are out"<<endl;



BUT if the user puts other than MARRIED im putting another statement there. My question is if the IF statement i used is correct? And what i should replace/add if the user adds another answer other than single?

Aug 20, 2014 at 10:45am
What do you mean by less than single?
Aug 20, 2014 at 10:54am
I don't know what exactly your problem.
There are some mistakes in your program:
1:IF should be written by small letters
2:you can't enter a string when "sg" is defined as an integer
Also I wrote a code you can't take a look at it and tell me if it works true.


int main()
{
int a;
cout<<"Enter a number(Married:1 Single:2):";
cin>>a;
if (a==1)
cout<<"You are married.";
else if (a==2)
cout<<"You are single.";
else
cout<<"Error";
return 0;
}
Aug 20, 2014 at 10:56am
#include <iostream>
#include <string>

using namespace std;

int main()
{
int sg;
cout << "1.Single\n2.Married\n";
cout << "Please enter relationship status:";
cin >> sg;

switch (sg)
{
case 1:
cout << "You are not married\n";
break;

case 2:
cout << "You are not single\n";
break;
default:
cout << "Choose 1 or 2";
cin >> sg;
}

system("pause");
return 0;
}
Last edited on Aug 20, 2014 at 10:57am
Aug 20, 2014 at 10:58am
My question is if the IF statement i used is correct?

No. In this case, the compiler is going to treat single as a variable, but it hasn't been declared, so you'll get an error.(Use double quotes for single. As in "single")
2. sg is of type int, so it cannot accept strings.(Change the type to std::string)
3. cout is used for output purposes, and cin is for input.
1
2
cout<<"Please enter relationship status: ";
cin>>sg;



BUT if the user puts other than MARRIED im putting another statement there.

You can use the std::to_lower() function (the name might not be correct)
if(std::to_lower(sg)=="single") .....

And what i should replace/add if the user adds another answer other than single?

Use an else block.
1
2
if(/*condtion*/){}
else{/*do something*/}


Aceix.
Aug 20, 2014 at 12:13pm
Thanks guys.
Also the =="single" worked and cout first before cin. I missed out on that. :(
and the additional brackets too.

And i cant enter the <<sg<<endl; in the cout right?

This actually just a small part of the long~~~~~ activity i have to do tomorrow.

I have so many errors errrrrr

Thanks for the help. Finally some light to this brain bulb of mine.
Topic archived. No new replies allowed.