Basic Input/Output - cin and strings problem

Hello everybody,
I'm new here..

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
Last edited on
i typed john then bears and everything worked as expected.
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;

Tested this and it works fine under gcc on linux. I don't see why you would be getting that kind of error.
I can't tell you why... VC6 does have some issues on XP and W7. Have you applied the "Visual C++ 6.0 Processor Pack" ?
http://msdn.microsoft.com/en-us/vstudio/aa718349.aspx

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...
Ah, so that's it.

I'm glad you solved it by updating to a modern compiler. You'll get much better mileage out of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <ctime>
#include <iostream>
using namespace std;

#include <windows.h>

bool iskeypressed( unsigned timeout_ms = 0 )
  {
  return WaitForSingleObject(
    GetStdHandle( STD_INPUT_HANDLE ),
    timeout_ms
    ) == WAIT_OBJECT_0;
  }

int main()
  {
  bool b = true;
  const char* s[ 2 ] = { "\r:-)", "\r;-)" };

  do cout << s[ b = !b ] << flush;
  while (!iskeypressed( 500 ));

  FlushConsoleInputBuffer( GetStdHandle( STD_INPUT_HANDLE ) );
  cout << endl;
  return 0;
  }
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 :-)
Watch closely. ;-)
when I compiled that code all it did is printed this:

:-)
Press any key to continue . . .


I had to run the .exe myself to eventually see the blinking face
Any idea why when I press Ctrl+F5 from Visual studio it wont blink?
Hmm, that's odd. I didn't expect it to fail in VS.

It is winking BTW, not blinking. :-\
i coudnt get the wink to work either. in debug it skips past it and as a release the window just flashes and vanishes. im using codeblocks

*edit it does work if i launch it as a release exe and launch it from a windows command prompt.
Last edited on
Topic archived. No new replies allowed.