C++ console decision making code doesn't work?

From the book C++ How To Program by Deitel 7th edition page 91

Page 1 of 1

4 Replies - 1312 Views - Last Post: 22 September 2010 - 10:35 AM Rate Topic: -----

#1 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

C++ console decision making code doesn't work?

Posted 22 September 2010 - 08:23 AM

Hey its been a long time since I visited this site maybe more than 2 years! anyways I registered structured programing CSC201 & one of the first things we learn is to make decision making code like addition subtraction etc.

anyways i'm using Microsoft Visual C++ 2010 express edition

I clicked new project named it "TEST" anyways I clicked win32 console application & I disabled precompiled header.

& I have written this code that is in the book in page 91

// test.cpp : Defines the entry point for the console application.
//

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
	int number1;
	int number2;

		cout<<"enter two integers two compare:";
		cin>>number1>>number2;

		if (number1==number2)
			cout<<number1<<"=="<<number2<<endl;
		
		if (number1 !=number2)
			cout<<number1<<"!="<<number2<<endl;
		if (number1 < number2)
			cout<<number1<<"<"<<number2<<endl;
		if (number1 >= number2)
			cout<<number1<<">="<<number2<<endl;

}



In the book its looks as if it worked fine without errors but when I run the code it says there are errors do you want to continue I click yes.

After that I type 2 random numbers & then the console disappears!!!!!!!!!

I wrote exactly what the book said what did I miss?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ console decision making code doesn't work?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • Posts: 10,784
  • Joined: 08-August 08

Re: C++ console decision making code doesn't work?

Posted 22 September 2010 - 08:29 AM

You could put another cin at the bottom to stop VS from closing the console when the program finishes, or you could put break points in. I use XCode, so I don't know the other methods to control VS.
Was This Post Helpful? 0
  • +
  • -

#3 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: C++ console decision making code doesn't work?

Posted 22 September 2010 - 08:49 AM

You forgot this:
return 0;



Although if you want to halt the screen you could write this before the return 0 statement at the end:
cin.ignore(cin.rdbuf()->in_avail()+1);

This post has been edited by aaa111: 22 September 2010 - 09:03 AM

Was This Post Helpful? 1
  • +
  • -

#4 busta5000  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: -7
  • View blog
  • Posts: 147
  • Joined: 08-March 08

Re: C++ console decision making code doesn't work?

Posted 22 September 2010 - 09:03 AM

:bigsmile: it worked adding

cin.ignore(cin.rdbuf()->in_avail()+1);
 return 0;



to my code made it work thanks aaa111

This post has been edited by busta5000: 22 September 2010 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: C++ console decision making code doesn't work?

Posted 22 September 2010 - 10:35 AM

View Postaaa111, on 22 September 2010 - 07:49 AM, said:

cin.ignore(cin.rdbuf()->in_avail()+1);


Nice trick, you're one of the few people I see using a proper cross-platform "pause" equivalent.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1