Incredibly simple code with an incredible list of errors

This is a discussion on Incredibly simple code with an incredible list of errors within the C++ Programming forums, part of the General Programming Boards category; I simply want the program to ask the name and then respond back using it, so I wrote this code: ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    16

    Incredibly simple code with an incredible list of errors

    I simply want the program to ask the name and then respond back using it, so I wrote this code:

    Code:
    #include <iostream>
    namespace std;
    
    int main()
    {
         cout<< "Hello, what is your name?";
         cin>> name;
    
         cout<< "Hello" << name << endl;
         
         return 0;
    }
    I don't even know why I'm using endl; or return 0; it just seems to be what you do, however, this program is giving me many errors (which I can post, but I didn't think it neccesary for this).

    The strangest error is it doesn't even seem to recognize namespace std, and I've no idea how I'm going to fix that issue.

  2. #2
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    you forgot:
    "using namespace std; "
    Hello, testing testing. Everthing is running perfectly...for now

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    You also didn't declare your name variable anyway.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    So it should be something like this?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    
    char name;
    
    {
         cout<< "Hello, what is your name?";
         cin>> name;
    
         cout<< "Hello" << name << endl;
         
         return 0;
    }
    Am I getting hotter or colder?

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You stayed about the same, you need to put the { after the int main() and before any variable declariations that are ment to be scoped within main.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
         char name;
    
    
         cout<< "Hello, what is your name?";
         cin>> name;
    
         cout<< "Hello" << name << endl;
         
         return 0;
    }
    Also, you need to look up how to properly make a string, if you want to use char for it... since char only stores one letter... there are is a whole tutorial at www.cprogramming.com read and follow all of the C++ tutorial, then come back and ask questions about your program if you still have any.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Hotter, but name isn't a single char. It should be an array of chars. The declaration also needs to go after the {.
    If you understand what you're doing, you're not learning anything.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    mildly hot, unless you want him to only show the first character of his name

    Also, it gives a compiler error because of the way the opening brace was placed.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    
         string name;
    
         cout<< "Hello, what is your name?";
         cin>> name;
    
         cout<< "Hello" << name << endl;
         
         return 0;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Ohhh...so it's a string because it's more than more char. Fantastic, fantastic. I think I understand something for once. Thanks everyone.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    Well that's the easy way out. I'd prefer using an array over using std::string in this case.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Why exactly? Using a char array is more likely to lead to a buffer overflow
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Generally you can place the braces however you feel like with a couple of exceptions.

    1.) The brace must come before anything else in the main function
    2.) You cannot put a matching brace i.e. the right curly brace before the starting brace left curly

    other than that usually, if its a good compiler, it shouldn't care about whitespace at all

    in regards to char arrays I agree with JaWib, std::string should be generally much more preferable than char arrays. Its much safer, and once you get used to it, almost as easy to use.
    Last edited by dpro; 06-26-2006 at 03:56 PM.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Well that's the easy way out. I'd prefer using an array over using std::string in this case.
    You'd prefer the harder way? Even if you do, that doesn't sound like good advice for somebody else.

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    Another problem (and this has been happening a lot, even with programs found in tutorials), the prompt comes up asking for my name, but once I put it in an press enter, it closes. I tried putting in a cin.get() after the cin>> name, but that just created more errors.

    Edit: Okay, I put a ";" after the "()" in cin.get(), and the errors are gone, but the program doesn't run any better than before I added it.
    Last edited by Twitchmokey; 06-26-2006 at 05:12 PM. Reason: debugging

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you get user input with cin >>, the newline that is put in the buffer from when the user presses enter is still there after you read in the data. The call to cin.get() gets that newline, instead of waiting for the user to hit enter again.

    The solution is to add cin.ignore() to ignore the newline. You can add it just before cin.get(), or you can add it after each call to cin >>, or you can do either of those but with cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n') to ignore all leftover characters just in case there are extras.

    Note that you will have the same issue if and when you use getline to read in an entire line (including spaces).

  15. #15
    Registered User
    Join Date
    Jun 2006
    Posts
    16
    I'm confused as to your explanation, but that didn't seem to work, at least not in the way I used it, here's the code as it stands now:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
        
         string name;
         
         cout<< "Hello, what is your name?";
         cin>> name;
         cin.ignore();
         cin.get();
         
    
         cout<< "Hello" << name << endl;
    }
    This time it waits for the user to press enter, but then it simply starts another line and terminates when enter is pressed again.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 03:09 AM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21