Help?

Posts 1–14 of 14 · Page 1 of 1
Help?
Well since im like 2 days old too c++ and i just learned cin and strings can someone help me i want to know how to make my command prompt answer to a yes or no question heres my source code

Code:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What's your last name? ";
    getline (cin, mystr);
    cout << "So its " << mystr << " ?\n";
    cout << "Do you like Aimbots? ";
    getline (cin, mystr);
    cout << "Well i havent figured out how to answer to yes or no questions yet im sorry ill get back to you later..\n";
    cout << "Its all about that MPGH.net\n";
    system ("pause");
    return 0;
}

please dont flame my post i am 2 days new to c++ so...

This was not my complete source code i have been learning from C++ Basic Tutorial
Quote Originally Posted by IamArtificial View Post
hmmmm thanks ive been learning from there :P
good then thats your next step. good luck
Quote Originally Posted by 'Bruno View Post
good then thats your next step. good luck
Thanks it is XD ^.^ heres my finishing thing it was surprisingly easy

Code:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What's your last name? ";
    getline (cin, mystr);
    cout << "So its " << mystr << " ?\n";
    cout << "Do you like Aimbots? ";
    getline (cin, mystr);
    if (mystr == "no")
    cout << "Well your gay...\n";
    if (mystr == "No"
    cout << "Well your gay...\n";
    if (mystr == "NO")
    cout << "Well your gay...\n";
    if (mystr == "yes")
    cout << "Its all about that MPGH.net\n";
    if (mystr == "Yes")
    cout << "Its all about that MPGH.net\n";
    if (mystr == "YES")
    cout << "Its all about that MPGH.net\n";
    system ("pause");
    return 0;
}

@Lilneo

all i did was the
Code:
if (mystr == "no") cout << "message here\n"
Quote Originally Posted by IamArtificial View Post
Thanks it is XD ^.^ heres my finishing thing it was surprisingly easy

Code:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What's your last name? ";
    getline (cin, mystr);
    cout << "So its " << mystr << " ?\n";
    cout << "Do you like Aimbots? ";
    getline (cin, mystr);
    if (mystr == "no")
    cout << "Well your gay...\n";
    if (mystr == "No"
    cout << "Well your gay...\n";
    if (mystr == "NO")
    cout << "Well your gay...\n";
    if (mystr == "yes")
    cout << "Its all about that MPGH.net\n";
    if (mystr == "Yes")
    cout << "Its all about that MPGH.net\n";
    if (mystr == "YES")
    cout << "Its all about that MPGH.net\n";
    system ("pause");
    return 0;
}
nice, btw one tip.. instead of having all these if's with same result, you could do:

Code:
if(xxx = "xxx" || xxx2 = "xxx" || xxx3 = "xxx")
|| is the same as "OR", you can also use && which is "AND". But yea, keep practicing and follow that site tutorial well

edit: btw, search around the web for some exercises, so you can actually practice.
Quote Originally Posted by 'Bruno View Post
nice, btw one tip.. instead of having all these if's with same result, you could do:

Code:
if(xxx = "xxx" || xxx2 = "xxx" || xxx3 = "xxx")
|| is the same as "OR", you can also use && which is "AND". But yea, keep practicing and follow that site tutorial well

edit: btw, search around the web for some exercises, so you can actually practice.
Code:
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string mystr;
    cout << "What's your name? ";
    getline (cin, mystr);
    cout << "Hello " << mystr << ".\n";
    cout << "What's your last name? ";
    getline (cin, mystr);
    cout << "So its " << mystr << " ?\n";
    cout << "Do you like Aimbots? ";
    getline (cin, mystr);
    if (mystr == "no" || mystr == "No" || mystr == "NO" || mystr == "Hell No")
    cout << "Well then your gay..\n";
    if (mystr == "yes" || mystr == "Yes" || mystr == "YES")
    cout << "MPGH.net is where its at!!\n";
    system ("pause");
    return 0;
}
look good? i think it does XD
Quote Originally Posted by IamArtificial View Post
look good? i think it does XD
way better xP
I don't know if you've learned functions yet, but you can use this to convert strings to lowercase:

Code:
#include <windows.h>
#include <iostream>
using namespace std;

string strToLower(string convertme); //forward declaration so we can put the function behind main

int main()
{
	string a = "TLDFLSDF";
	a = strToLower(a); //convert the string to lowercase
	cout<<a.c_str(); //print the string as char array/c style string
	return 1;
}

string strToLower(string convertme)
{
	for(int i=0;i<(int)strlen(convertme.c_str()); i++)
	{
		convertme[i] = tolower(convertme[i]);
	}
	return convertme;
}
Quote Originally Posted by Hell_Demon View Post
I don't know if you've learned functions yet, but you can use this to convert strings to lowercase:

Code:
#include <windows.h>
#include <iostream>
using namespace std;

string strToLower(string convertme); //forward declaration so we can put the function behind main

int main()
{
	string a = "TLDFLSDF";
	a = strToLower(a); //convert the string to lowercase
	cout<<a.c_str(); //print the string as char array/c style string
	return 1;
}

string strToLower(string convertme)
{
	for(int i=0;i<(int)strlen(convertme.c_str()); i++)
	{
		convertme[i] = tolower(convertme[i]);
	}
	return convertme;
}

Its wierd you posted this i just got to functions last night but i stopped XD
so i am learning functions right now xP
No one is going to flame you for asking a question. Here is an example to answering a question:
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
 string input="";
 cout<<"Say yes or no\n";
 cin>>input;
 if (input=="Yes")
 {
	 cout<<"You said Yes\n";
 }
 if (input=="No")
 {
	 cout<<"You said No\n";
 }
 if (input!="No" && input!="Yes")
 {
	 cout<<"You didn't say yes or no";
 }
 system("pause");
 return 0;
}
Warning, you will have to type in exactly "Yes" or "No". You can use functions like "tolower()" to force all the characters to lower case so you can check for non case-sensitive inputs.

But anyways, I hope I could help. This compiled in MSVC++ 2008 Express edition.

~lilneo
Quote Originally Posted by lilneo View Post
No one is going to flame you for asking a question. Here is an example to answering a question:
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
 string input="";
 cout<<"Say yes or no\n";
 cin>>input;
 if (input=="Yes")
 {
	 cout<<"You said Yes\n";
 }
 if (input=="No")
 {
	 cout<<"You said No\n";
 }
 if (input!="No" && input!="Yes")
 {
	 cout<<"You didn't say yes or no";
 }
 system("pause");
 return 0;
}
Warning, you will have to type in exactly "Yes" or "No". You can use functions like "tolower()" to force all the characters to lower case so you can check for non case-sensitive inputs.

But anyways, I hope I could help. This compiled in MSVC++ 2008 Express edition.

~lilneo



Or you could use
Code:
if (input!="No" || "no" && input!="Yes" || "yes")
Not sure if this will work properly, but it allows for yes, Yes, no and No questions if it does work.
Quote Originally Posted by VvITylerIvV View Post
Or you could use
Code:
if (input!="No" || "no" && input!="Yes" || "yes")
Not sure if this will work properly, but it allows for yes, Yes, no and No questions if it does work.
No that wont work.

This should:

[php]
if (input != "no" || input != "No" && input != "Yes" || input != "yes")
[/php]

anyway i always hate this kind of ifs, but yea..
Quote Originally Posted by lilneo View Post
No one is going to flame you for asking a question. Here is an example to answering a question:
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
 string input="";
 cout<<"Say yes or no\n";
 cin>>input;
 if (input=="Yes")
 {
	 cout<<"You said Yes\n";
 }
 if (input=="No")
 {
	 cout<<"You said No\n";
 }
 if (input!="No" && input!="Yes")
 {
	 cout<<"You didn't say yes or no";
 }
 system("pause");
 return 0;
}
Warning, you will have to type in exactly "Yes" or "No". You can use functions like "tolower()" to force all the characters to lower case so you can check for non case-sensitive inputs.

But anyways, I hope I could help. This compiled in MSVC++ 2008 Express edition.

~lilneo
[php]string input="";[/php]
You dont need to initialize it
[php]string input;[/php]
is just fine
Posts 1–14 of 14 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us