Result not changing


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
30
31
#include "stdafx.h"
#include <iostream>
#include <limits>
using namespace std;


int main ()
{
	int a, b; // declaring variables
	int result;

	a = 5;
	b = 2;

	a = a + 1;
	result = a - b;

	cout << result << endl;

	cout << "Is this incorrect?" << endl;
	cout << "Enter correct result." << endl;
	cin >> "Result";
	cout << "Let me check again" << endl;
	

	std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;
	
}


The messages are me just practicing, to drill them into my brain, but why is it not matter what I change a or b to the result is always 10, and how can 10 be right? When I run this I get result = 10.

Debugging error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
c:\program files\microsoft visual studio 10.0\vc\include\istream(429): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(long double &)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>
          ]
          c:\program files\microsoft visual studio 10.0\vc\include\istream(447): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(void *&)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>
          ]
          c:\program files\microsoft visual studio 10.0\vc\include\istream(466): or       'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::operator >>(std::basic_streambuf<_Elem,_Traits> *)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>
          ]
          while trying to match the argument list '(std::istream, const char [7])'
Last edited on
cin >> "Result";

This is nonsense. You're extracting user input and putting it into a string literal. It's like doing this:

"Result" = 5;

How can you assign 5 to "Result"? "Result" isn't a variable.

You probably meant to do this:

cin >> result; // result is your variable
The result is 4, as it should be. You probably forgot to recompile or something.
The error is because you're trying to read a value from cin into a string literal in line 22 (string literals are constant). This shouldn't even compile, so you should check in your compiler options for an option to enable stricter error checking.

Edit: heh, ninja'ed again.
Last edited on
Okay I see, but what has the in/output messages got to do with changing the result, they are only messages? Now I get 4, but earlier I kept getting 10 until I took the cin/couts out of the code.
Maybe, since it wasn't compiling, you just kept running a previous version of the program (i.e. what code you saw actually wasn't getting executed). Once you removed the cin/couts, the code compiled correctly so then it started running the new code.
Trying to write into the "Result" string literal results in undefined behavior, which can mean a lot of things - from a simple crash to some some very strange program behavior such as this.
The compiler seeing you trying to input data into "Result" might have lead it to make some strange and invalid assumption while optimizing.

Still, it seems more likely that the program wasn't rebuilt properly after you changed the values of a/b to what they are now.
Does the incorrect result reappear when you include cin >> "Result";?

Edit: I'm not familiar with how Visual Studio handles things, but shacktar's theory certainly seems likely.
Last edited on
@ Shacktar, I dunno dude, it said it was compiling. But it is most likely as you say

@ Athar, If you don't use VS what do you use? Yes the incorrect result still appeared with cin >> Result
Topic archived. No new replies allowed.