CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: C++ Help Please

  1. #1
    Join Date
    Jul 2007
    Posts
    5

    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_

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: C++ Help Please

    You didn't provide a body for the function main.

  3. #3
    Join Date
    Jul 2007
    Posts
    5

    Re: C++ Help Please

    Huh, what do you mean could you please show me a example using my code?

    Thanks in advance


    ~Vash_

  4. #4
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    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.

  5. #5
    Join Date
    Jul 2003
    Location
    Linköping, Sweden
    Posts
    261

    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.

  6. #6
    Join Date
    Sep 2004
    Location
    New Delhi, India
    Posts
    640

    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!!!

  7. #7
    Join Date
    Jul 2007
    Posts
    5

    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
  •  





Click Here to Expand Forum to Full Width

Featured