cin.ignore and cin.get

When I run this code, I only have a chance to type in the variable "helTal" after that the program prints out:

OUTPUT:
Skriv in ett heltal:32 //This line works as I want to, when I hit enter it prints out the OUTPUT part after this line.

En textsträng: Ett decimaltal: Heltalet är: 32
Textsträngen är:
Decimaltalet är: 6.95322e-310
Vill du prova igen tryck 1, annars 0:
Tack för besöket!

CODE:
#include <iostream>
using namespace std;

int main ()
{
int helTal, meny;
char tecken[30];
double decimalTal;
do {
cout << "Skriv in ett heltal: ";
cin >>helTal;

cout << "En textsträng: ";
cin.get (tecken,30);
cin.ignore (30, '\n');

cout << "Ett decimaltal: ";
cin >> decimalTal;

cout <<"Heltalet är: " << helTal << endl;
cout << "Textsträngen är: " << tecken << endl;
cout << "Decimaltalet är: " << decimalTal << endl;
cout << "Vill du prova igen tryck 1, annars 0:\n";
cin >> meny;
}
while (meny == 1);
cout << "Tack för besöket!";
return 0;
}
I'll edit this when I've finished reading it but for starters, why is there text in Swedish? What's in the cout doesn't matter, but for variables, it'd help others with appropriate English names.

Oh, and wrap your source code in tags: [ code ] source code here... [ /code ] without space between brackets.

1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
  std::cout << "Hello World!";

  return 0;
}


EDIT: Browni3141 beat me to it ^^

As for the Swedish part, it's just for future reference. I like to think of it as common courtesy to make it easier for those who take their time to help you =)
Last edited on
why is there text in Swedish?

I'm guessing it's because he's Swedish. Some people on this forum might know some, and it's a short program so it's not a big deal.

Edit: I think you need cin.ignore(30, '\n') before cin.get(tecken, 30) instead of after.
Last edited on
I'll try to learn to wrap my code right, hope I did it right now. And like Browni say I thought that this is so simple code that I didn't need to translate but I'll give it a try now.

OUTPUT:
Type an integer:32 //This line works as I want to, when I hit enter it prints out the OUTPUT part after this line.

Type a string with two or moore words: Type a double: The Integer is: 32
The string is:
The Doble is: 6.95322e-310
What to try again press 1, else press 0:
Thank you!!

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
27
28
29
#include <iostream>
using namespace std;

int main ()
{
int aInteger, meny;
char aChar[30];
double aDouble;
do {
cout << "Type an integer: ";
cin >>aInteger;

cout << "Type a string with two or moore words: ";
cin.get (aChar,30);
cin.ignore (30, '\n');

cout << "Type a double: ";
cin >> aDouble;

cout <<"The Integer is: " << aInteger << endl; 
cout << "The string is: " << aChar << endl;
cout << "The Doble is: " << decimalTal << endl;
cout << "What to try again press 1, else press 0:\n";
cin >> meny;
}
while (meny == 1);
cout << "Thank you!";
return 0;
}
Btw are you studying on your own or is this part of school?
I'm 44 years old so I study most for fun, but it is a real education i'm doing on distance. I have a full time work in a softwarecompany as a salesman and projectleader.
So I though it would be nice if I get some understanding about what my programmers do. My goal is to try some Objective-C on the Mac in the future.

About my code, I have changed place on
1
2
cin.get(aChar, 30); 
cin.ignore (30, '\n');

to
1
2
cin.ignore (30, '\n');
cin.get(aChar, 30); 


Then it works but I'll be very glad if someone will explain why for me.
Ah, I see. Is that a high school-level course, or is it a university/college course? Or is it provided through another company (educational or otherwise)? While mostly interesting in computer game development, I would like to try my hands on Objective-C in the future as well as AppleScript. I own both a MacBook and an iPhone, might as well take advantage of that and the programming...

Anyway, about your querie: When you first input data through cin >> and press the return key, you leave behind a '\n' character. You'll want to use cin.ignore() to tell the program to ignore that character. You might also want to ask yourself why you want to put 30, '\n' or both inside the parentheses of cin.ignore().

I know my answer is incomplete. Hope this helps for now, though.
Thank you!

The course is at high school-level.
I tried cin.ignore() without any parameters and that work nice to.
Ah, so you're doing the A-course. Are you planning to do the B and C-course as well?
Yes, that's my plan if I work this first course out. I study in 25% speed so my weekends are all about reading and coding. I use Apples Xcode to do my coding, works fine for me and is good for the future as my plan is continue with Objective-C.
I'm using Xcode as well. I like it a lot, find it much easier to use than Eclipse or NetBeans, for instance.

So we're both doing the same thing then. =) Well, best of luck to you! I'm doing the C-course full-time right now, and it's a huge step-up from the B-course. The magnitude of the C-course is almost ridiculous.
Do you need to scare me like that... ;-)
Best of luck to you to!
Topic archived. No new replies allowed.