c++ - if (cin >> number_1) doesn't work -


im reading c++ book called programming principles , practice using c++.

and im in chapter drills if statments.

one drill make when type 1-4 says in letters. 1 = 1 , 3 = 3 , on. can't make correct if statment seems.

here code :

#include "stdafx.h" #include<iostream> #include<string> #include<vector> #include<algorithm> #include<cmath> using namespace std; inline void keep_window_open() {     char ch; cin >> ch; }   int main() {     cout << "enter 0, 1, 2, 3 or 4\n";     int number_0 = 0;     int number_1 = 1;     int number_2 = 2;     int number_3 = 3;     int number_4 = 4;        if (cin >> number_0){         cout << "zero";     }      if (cin >> number_1){         cout << "one";     }      if (cin >> number_2){         cout << "two";     }      if (cin >> number_3){         cout << "three";     }      if (cin >> number_4){         cout << "four";     }      keep_window_open();  } 

any appreciated!

your understanding of cin skewed.

cin read value input stdin (in case keyboard) variable.

cin >> number_0; 

reads value number_0. do

int x; cin >> x; if(x == 0) {     cout << "that's zero!" << endl; } 

or even:

const int number_0 = 0; //store 0 in case math changes if(x == number_0) {     cout << "that's zero!" << endl; } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -