|
-
July 24th, 2007, 09:49 AM
#1
C++ Help Please
Hello, heres my code i'm currently ran into some problems and i cant get it to work.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
int dan
{
string mystr;
std::cout << "What's your name? ";
getline (cin, mystr);
if (mystr == dan;)
cout << "My name is" << mystr << "too";
std::cout << "Hello " << mystr << ".\n";
std::cout << "What is your favorite team? ";
getline (cin, mystr);
std::cout << "I like " << mystr << " too!" "\n" << std::endl;
std::cout << "My name is Dan and i'm starting using C++!" "\n" << std::endl;
std::cout << "Look forward to see more Programming from me :D." "\n" << std::endl;
return 0;
}
Could someone please tell me why this will not Compile. Heres the Errors that i got back i cant fix them maybe you could help?
Errors:
Code:
main.cpp:8: error: expected init-declarator before "int"
main.cpp:8: error: expected `,' or `;' before "int"
P.S I'm using Code::Blocks if that helps.
Thanks in advance.
Vash_
-
July 24th, 2007, 09:58 AM
#2
Re: C++ Help Please
You didn't provide a body for the function main.
-
July 24th, 2007, 10:03 AM
#3
Re: C++ Help Please
Huh, what do you mean could you please show me a example using my code?
Thanks in advance
~Vash_
-
July 24th, 2007, 10:10 AM
#4
Re: C++ Help Please
It's worse than that. I think the body that is supplied is meant to be for main() and that dan isn't meant to be a function, what is more, it looks like dan isn't even meant to be an int, but a string.
I haven't tried compiling it, but try the following:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string dan = "Dan";
string mystr = "";
cout << "What's your name? ";
getline (cin, mystr);
if (mystr == dan)
{
cout << "My name is" << mystr << "too";
}
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n" << endl;
cout << "My name is Dan and i'm starting using C++!\n" << endl;
cout << "Look forward to see more Programming from me :D.\n" << endl;
return 0;
}
Note that you need to input 'Dan' in order for the comparator to work ('dan' will fail because the string comparator is case sensitive). Also, if you are using namespace std, you do not need to specify 'std::' in front of 'cin', 'cout', 'string' and 'endl' in your program.
-
July 24th, 2007, 10:12 AM
#5
Re: C++ Help Please
Your main function has no body. Functions definitions are made on the form type name(type argument1, type argument2...){ statement1; statement2;... return value;} where statement1 ... return value are the body of the function.
Before what I assume you meant to use as the body of int main(), you have placed int dan. If you meant for dan to be a local variable in main, you must put it in the body (that is, inside the {} brackets).
Errare humanum est, ergo non sum humanus.
-
July 24th, 2007, 10:12 AM
#6
Re: C++ Help Please
Code:
#include <iostream>
#include <string>
using namespace std;
int dan ();
int main()
{
//Let's call our function
dan ();
return 0;
}
int dan ()
{
string mystr;
std::cout << "What's your name? ";
getline (cin, mystr);
if (mystr == "dan"/*String literals come in quotes"*/)
cout << "My name is" << mystr << "too";
std::cout << "Hello " << mystr << ".\n";
std::cout << "What is your favorite team? ";
getline (cin, mystr);
std::cout << "I like " << mystr << " too!" "\n" << std::endl;
std::cout << "My name is Dan and i'm starting using C++!" "\n" << std::endl;
std::cout << "Look forward to see more Programming from me :D." "\n" << std::endl;
//No need to return int if you don't need it
//You can make the function to be void dan ()
return 0;
}
"I rather not play football than wear Nerrazzuri shirt" - Paolo Maldini
FORZA MILAN!!!
-
July 24th, 2007, 10:21 AM
#7
Re: C++ Help Please
Thanks for the fast help guys! Works perfect now. 
Sorry if it was a n00b (Newbie) question, but i've only just started learning C++.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|