I'm trying to follow the C++ Language Tutorial but got stuck on Basic Input/Output chapter.
Here is the simple code that's not behaving as it should:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
when I execute this it behaves very strange, it goes like this:
It ask me what is my name and waiting on my input
I type _my name_ and press enter, then it jumps on the next line and wait for another input, I type _something_ and press enter again.
the program then say Hello _my name_ and asks me what is my favorite team and wait for another input. I type _team_ and press enter.
It shows I like _something_ too and the program ends.
so my question is why does it behaves like this when it clearly should do something different?
why does it asks me for 3 inputs when I have only 2 in my program?
I'm a little confused with this and trying to figure out what am i doing wrong.
BTW I use Microsoft Visual C++ 6.0 on win7 64bit if that could make any difference
Would you mind editing your post and putting the source inside code tags? It will make it a lot more legible and folks here will be more likely to look at it.
Does changing this line: cout << "Hello " << mystr << ".\n";
to this have any impact? cout << "Hello " << mystr << "." << std::endl;
Another possibility is that there is nothing wrong with your program at all... but there may be something wrong with the environment in which it is running.
If you just run your program from the IDE or using the default Windows Console there may be compatability issues. Try creating a shell with compatability mode for Windows 2000.
I can't get MSVC to install on my PC so I can't help you any more than that... Sorry.
2_PanGalactic: nope, that didn't do anything and the program still did the same strange thing
2_Duoas: I have tried to install that processor pack but it didnt installed because I need service pack 5.0 which I could not find anywhere to download.. I did find and installed service pack 6.0 though but that processor pack still didn't want to install.
I also tried and run VC6 in compatability mode (win2000) but when I try and open my workspace it crashes everytime.
Hovever I found a KB article on MS website that says VC 6.0 is not compatible with 64bit operating systems and even if it can run fine, there might be some issues that it wont run as intended...
so I guess I will just need a new compiler as I remember when I first installed VC6.0 win7 said that there are known compatability issues but I ignored that and it worked (untill now)
I did try and install visual studio 2010 where this code runs normally. So I'm going to use this one to continue with my learning...
I was afraid from the idea to install the whole visual studio and that I would be totaly lost in it... but It wasn't so bad. I was able to open my win32 console project in c++ so everythings looks fine.... even tho i have a few GBs of space less on my hdd now and still all I can do is the simple "hello world" programs only.. :)
btw is that code of yours supposed to do something more than just print a single smilie face?
I see 2 of them in the code but only one is displayed when I run it. Don't get the lines 7-13 at all... such a complicated way to print a :-)