Followers 0

# strange occurence mittle piecec++ code I wrote

## 10 posts in this topic

Hello everybody,

I have started with studying C++ and it goes very well since I never have learned a program language. I studied from the e-book juming into C++ which is a very good ebook for beginners.

Now I have wrote a piece of code and when I put in the numbers it is going good. But <hen I do a test and as a first number or in dutch type eerste number and then an a or b I get a very strange result..Can somebody explain why he is doing that?

the code is:

#include <iostream>
using namespace std;
int main ()
{
int eerste_liefdes_nummer;
int tweede_liefdes_nummer;

cout << "type eerste liefdes nummer: ";
cin>> eerste_liefdes_nummer;
cout << "type tweede liefdes nummer: ";
cin>>tweede_liefdes_nummer;
cout << eerste_liefdes_nummer << " * " << tweede_liefdes_nummer << " = " << eerste_liefdes_nummer * tweede_liefdes_nummer << endl;
cout << "jullie zijn heel erg verliefd";
}

Patrick
0

##### Share on other sites
Do you mean you literally type "a" or "b" instead of a number?

If that's the case, then your input fails because a number is expected. Let's say you type "a" letter instead of a number for the first number. The input is incorrect and the input fails, leaving the first variable unchanged and leaving the letter "a" in the input stream. When reaching the second input, the input stream still contains the letter "a" and you're trying to read the second number. This also fails, leaving the second variable unchanged, and again leaving the letter "a" in the input stream.

Now you have to variables with no particular value, and you proceed to print them.
0

##### Share on other sites
Als eerste welkom hier.

What you ran into happens because you don't initialise:
[code]int eerste_liefdes_nummer;
int tweede_liefdes_nummer;[/code]

In C++ when you don't initialise a variable it will just contain random data, or to put this a bit more correctly just the data the that's in the location assigned to the variable.
0

##### Share on other sites
no, it does the following thing otherwise paste the code and test it....

type eerste liefdes nummer: a (I type a instead of a number and press enter)

the result is:type tweede liefdesnummer: 0 * 49 = 0 (that is what it puts on the screen after the enter of the first a

nightcreature..are you speaking dutch?spreek je nederlands?
0

##### Share on other sites
I dont know whats wrong but ill just list what i understood the problem to be:
1.You type "a" for the first number
2.You press enter

then it:
-completely skips waiting for the second number
-puts 49 in the second number, not the first number where it should be?
-tells you what they are multiplied (with the first number 0)
0

##### Share on other sites
Okay, but where does he get this number 49?
0

##### Share on other sites
I forgot to mention this, but a common shortcut for reading a value from a stream and checking it succeeded is to place the read operation as the conditional expression:

For example:
[code]
if(cin >> someVariable) {
}

// or

while(getline(cin, line)) {
}
[/code]
1

##### Share on other sites
What does it return? Char count? [/lazymode]

Nvm, I looked it up. Returns .good(). Nice to know. Edited by Khatharr
0

##### Share on other sites
Robust input from the user is surprisingly hard to do, as you can probably gather from rip-off's post. I rarely write programs where the user types anything in (my programs read command-line options and configuration files instead) but when I do, I always read the input line by line with std::getline(std::cin, line)' and then parse the line. In my experience it's the safest way to do things.

Since you are a beginner, it is likely that rip-off's code is too complicated for you at this stage. If that's the case, skip this whole robust-input thing and get on with learning the basics of the language (just be careful when you type ;) ). Once you are more comfortable with the language, come back and read his very useful code.
1

##### Share on other sites
[quote name='patisake' timestamp='1352898995' post='5000897'] no, it does the following thing otherwise paste the code and test it.... type eerste liefdes nummer: a (I type a instead of a number and press enter) the result is:type tweede liefdesnummer: 0 * 49 = 0 (that is what it puts on the screen after the enter of the first a nightcreature..are you speaking dutch?spreek je nederlands? [/quote]

Ja, ik spreek Nederlands woon er alleen niet meer.

[quote name='Álvaro' timestamp='1352905742' post='5000926'] Robust input from the user is surprisingly hard to do, as you can probably gather from rip-off's post. I rarely write programs where the user types anything in (my programs read command-line options and configuration files instead) but when I do, I always read the input line by line with std::getline(std::cin, line)' and then parse the line. In my experience it's the safest way to do things. Since you are a beginner, it is likely that rip-off's code is too complicated for you at this stage. If that's the case, skip this whole robust-input thing and get on with learning the basics of the language (just be careful when you type ;) ). Once you are more comfortable with the language, come back and read his very useful code. [/quote]
I found using JSON or XML parsers to be easier then using ini or text files. Also gives you the ability of skipping whole blocks if the code doesn't understand the content.
0

## Create an account

Register a new account