#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2

    Having trouble with strings in Visual C++


    Hello,

    I recently have been teaching myself Visual C++, using the Visual Studio 2008 program since I'm on a computer with Windows XP.

    Anyway, I've been trying to complete a set of tutorials little by little, and have been having some difficulties with using strings.

    For some reason, I keep getting errors, even though the code is verbatim to the video tutorial.

    Here is my program:

    #include <iostream>
    #include <string>
    //this is a library file apparently


    using namespace std;

    //This tells the program to "use" the type of file called a namespace

    int main()
    {
    string name;

    cout << "Hello, what is your name?";

    cin >> name

    cout << "Well, hello there, "; << name << endl;

    return 0;

    }

    It says I should include a semi-colon before cout... which doesn't make sense at all.

    Any thoughts on what could be wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    68
    Rep Power
    5
    Remember that every statement must end in a semicolon. The compiler's just telling you that somewhere you forgot to end a line with one.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Ah good point, but even when I added the semi-colon after the cin statement, its still giving me a semi-colon error...

    Hmm..
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    Look at the line number in the error/warning then use that number to go to the offending line in your code.

    cout << "Well, hello there, "; << name << endl;
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Oh cool, it totally works!

    Thanks so much!

    I also did a simple thing with arrays, but is giving me another semi-colon error.

    Here's the program:

    #include <iostream>

    using namespace std;

    int main(){

    int numberArray(5) = (1, 2, 3, 4, 5);

    cout << numberArray(0) << endl;

    return 0;

    }
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,388
    Rep Power
    1871
    Read your book again.
    Arrays use [ ] to declare dimensions and element access.
    Arrays also use { } for initialisers.

    You've used ( ) all over the place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Oh okay, thanks a bunch! I'll try that to see if it works.

    I should have paid more attention, my bad.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Originally Posted by salem
    Read your book again.
    Arrays use [ ] to declare dimensions and element access.
    Arrays also use { } for initialisers.

    You've used ( ) all over the place.
    Alright, I definitely fixed it, and it runs, but only a brief second.

    It just shows the black console window for a second and exits the program.

    I realized with other programs I made thus far, that my main enter key really does a hard enter, twice in a row, thus i used the numpad instead with better results for inputting data.

    But, in this case, I don't know why its only running for a second haha. Pretty weird.

    Here's the code thus far, with the revisions like you mentioned:

    #include <iostream>

    using namespace std;

    int main(){

    int numberArray[5] = {1, 2, 3, 4, 5};

    cout << numberArray[0] << endl;


    return 0;

    }
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    There's a sticky thread, Commonly Asked C/C++ Questions (that's a link, BTW). It contains questions that keep getting asked over and over again. It's there for everybody to check first to see whether their own "new" question is one of the old questions.

    Does 0.3 I cannot see my C compiler output. The window is closing too quickly for me. look at all familiar?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    232
    Rep Power
    2
    Oh okay, thanks, I'll be sure to check the FAQ from now on.

    I think I checked it before, but it was a bit on the specific side for more advanced topics.

    Thanks man.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    26
    Rep Power
    0
    maybe you have to use system("PAUSE");in the main function just before return statement

IMN logo majestic logo threadwatch logo seochat tools logo