| View previous topic :: View next topic |
| Author |
Message |
magiadam Expert Cheater
Reputation: 0
Joined: 02 Jan 2008 Posts: 105
|
Posted: Mon Feb 25, 2008 6:24 pm Post subject: First program Ever Made Help |
|
|
#include <iostream>
using namespace std;
int main() {
name, answer;
cout << "Hello there, my name is Adam.";
cout << "What is your name?";
cin >> name;
cout << "Hello " << name;
cout << "Tell me " << name;
cout << "Do you love me?";
cin >> answer;
if (answer == yes)
cout << "Aaw, I love you too ";
else
cout << "I don't love you at all.";
return 0;
}
Gives me a compile error with my variables "name, answer", any help would be nice D: |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Feb 25, 2008 6:34 pm Post subject: |
|
|
| Code: | #include <iostream>
char name [255];
char answer;
int main(void){
std::cout << "Hello there, my name is Adam, what is your name? ";
std::cin >> name;
std::cout << "Hello " << name << ", do you love me? ";
std::cin >> answer;
switch(answer){
case 'y':
case 'Y':
std::cout << "Aww, I love you too!";
break;
default:
std::cout << "I don't love you at all.";
break;
}
return 0;
} |
And to actually answer your questions... you're not defining answer and name as anything.
Also you can't just compare answer like that. In my case I'm just assuming that if the first letter is "y" that it's yes, and if it's not, then no. |
|
| Back to top |
|
 |
magiadam Expert Cheater
Reputation: 0
Joined: 02 Jan 2008 Posts: 105
|
Posted: Mon Feb 25, 2008 6:44 pm Post subject: |
|
|
Error C2001: newline in constant.
And I haven't learned it yet, but why was "case 'Y':" inserted?
Thanks. |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Feb 25, 2008 6:54 pm Post subject: |
|
|
| So they could use a capital or lowercase. |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Mon Feb 25, 2008 6:59 pm Post subject: |
|
|
| Code: | #include <iostream>
char name [255];
char answer;
int main(void)
{
std::cout << "Hello there, my name is Adam, what is your name\? " << std::endl;
std::cin >> name;
std::cout << "Hello " << name << ", do you love me\? " << std::endl;
std::cin >> answer;
switch(answer)
{
case 'y':
case 'Y':
std::cout << "Aww, I love you too!";
break;
default:
std::cout << "I don't love you at all.";
break;
}
std::cin.sync();
std::cin.ignore();
return 0;
} |
dont forget ? has to be inserted with an escape sequence: \?
and also dont forget to add new lines. and remember to add a cin.ignore/sync statement at the end so the console doesnt kill itself before ur able to read it.
the switch statement is just neater replacement for multiple if/else statements.
the equivilent would be
| Code: | if ( answer != 'y' || answer != 'Y' )
std::cout << "i hate you";
else
std::cout << "i love you too"; |
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Feb 25, 2008 7:05 pm Post subject: |
|
|
Can also use strcmp
| Code: |
#include <iostream>
int main(void){
char* name = new char[255];
char* answer = new char[255];
std::cout << "Hello there, my name is Adam, what is your name? ";
std::cin >> name;
std::cout << "Hello " << name << ", do you love me? ";
std::cin >> answer;
if(strcmp(answer, "Yes") == 0){
std::cout << "Aww, I love you too!";
}
else{
std::cout << "I don't love you at all.";
}
return 0;
} |
|
|
| Back to top |
|
 |
magiadam Expert Cheater
Reputation: 0
Joined: 02 Jan 2008 Posts: 105
|
Posted: Mon Feb 25, 2008 7:13 pm Post subject: |
|
|
| I pressed "Ctrl + F5" and "Ctrl + Shift + B" and it doesn't create an .exe on my Visual C++ 2008. How do you make the .exe when you're finished? (Sorry for the newb questions, just bought my first C++ book this morning XD). |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Feb 25, 2008 7:16 pm Post subject: |
|
|
| are you looking in: my documents -> visual studio 2008 -> projects -> name |
|
| Back to top |
|
 |
magiadam Expert Cheater
Reputation: 0
Joined: 02 Jan 2008 Posts: 105
|
Posted: Mon Feb 25, 2008 7:18 pm Post subject: |
|
|
1>------ Build started: Project: FirstProg2, Configuration: Debug Win32 ------
1>Compiling...
1>FirstProg2.cpp
1>.\FirstProg2.cpp(1) : error C2001: newline in constant
1>Build log was saved at "file://c:\Documents and Settings\Adam\My Documents\Visual Studio 2008\Projects\FirstProg2\FirstProg2\Debug\BuildLog.htm"
1>FirstProg2 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So there must be an error in the script o__o
And
| slovach wrote: | | are you looking in: my documents -> visual studio 2008 -> projects -> name |
Yes I did, nothing. |
|
| Back to top |
|
 |
|