very noob c++ question

KAMAZON

Golden Member
Apr 4, 2001
1,300
0
76
www.alirazeghi.com
Hello, I am doing a simple program out of my text book (it's not required by the school but I want to learn it) and whenever I input a inch value on the compiled program, it gives me some long ass scientific number. Here is the code. I am using Visual Studio C++ 6


int main()
{
//declaring data types

int feet;
int inches;
int totalInches;
double centimeters;

// output and input for inches
cout << "Enter two integers, one for feet and "
<< "one for inches: ";

cin >> feet, inches;
cout << endl;
cout << "The numbers you entered are " << feet
<< " for feet and " << inches
<< " for inches." << endl;

// calculating inches
totalInches = INCHES_PER_FOOT * feet + inches;


cout << "The total number of inches = "
<< totalInches << endl;

// calculating centimeters
centimeters = CENTIMETERS_PER_INCH * totalInches;

cout << "The total number of centimteres = "
<< centimeters << endl;
return 0;
}


Here is the output. Do note I enter 1 1 for the inputs:

Enter two integers, one for feet and one for inches: 1 1

The numbers you entered are 1 for feet and -858993460 for inches.
The total number of inches = -858993448
The total number of centimteres = -2.18184e+009
Press any key to continue

I also get this warning when I compile it:
d:\program files\microsoft visual studio\myprojects\test2\test2.cpp(28) : warning C4700: local variable 'inches' used without having been initialized


Thank you!
 

KAMAZON

Golden Member
Apr 4, 2001
1,300
0
76
www.alirazeghi.com
Well I don't know why what I did worked but I re-wrote it and it works now. Here is what I am using now:

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

#include "iostream"
using namespace std;


int main()
{
//declaring data types

int feet;
int inches;
int totalInches;
double centimeters;


const double CENTIMETERS_PER_INCH = 2.54;
const double INCHES_PER_FOOT = 12;

// output and input for feet
cout << "Enter 1 integer for feet: ";
cin >> feet;
cout << "You have entered " << feet << " feet." << endl;

// output and input for inches
cout << "Enter 1 integer for inches: ";
cin >> inches;
cout << "You have entered " << inches << " inches." << endl;

cout << "The numbers you entered are " << feet
<< " for feet and " << inches
<< " for inches." << endl;

// calculating total inches
totalInches = INCHES_PER_FOOT * feet + inches;

cout << "The total number of inches = "
<< totalInches << endl;


// calculating centimeters
centimeters = CENTIMETERS_PER_INCH * totalInches;


cout << "The total number of centimteres = "
<< centimeters << endl;

return 0;
}
 

DanceMan

Senior member
Jan 26, 2001
474
0
0
Originally posted by: KAMAZON
Sir, I don't care what anyone says about you. You are a gentleman and a scholar, thank you very much!

This made me think of something, that I had to google to see if I remembered correctly, and I did...

Let's just say that he initialized inches = 0. Then the warning would not have been generated. So, what really happened? In C++ (and C) there's the comma operator, and although it was an improper use of the cin, the comma operator was the real culprit here.

I had to check this, mainly because I've been doing all Java programming lately, and I had to see if it was still alive and kicking in Java. It is, but only for FOR loops.
 

sao123

Lifer
May 27, 2002
12,650
203
106
Originally posted by: KAMAZON
Hello, I am doing a simple program out of my text book (it's not required by the school but I want to learn it) and whenever I input a inch value on the compiled program, it gives me some long ass scientific number. Here is the code. I am using Visual Studio C++ 6


int main()
{
//declaring data types

int feet;
int inches;
int totalInches;
double centimeters;

// output and input for inches
cout << "Enter two integers, one for feet and "
<< "one for inches: ";

cin >> feet, inches; \\You cant do this...
cin >> feet >> inches; \\Proper coding of this line...
cout << endl;
cout << "The numbers you entered are " << feet
<< " for feet and " << inches
<< " for inches." << endl;

// calculating inches
totalInches = INCHES_PER_FOOT * feet + inches;


cout << "The total number of inches = "
<< totalInches << endl;

// calculating centimeters
centimeters = CENTIMETERS_PER_INCH * totalInches;

cout << "The total number of centimteres = "
<< centimeters << endl;
return 0;
}


Here is the output. Do note I enter 1 1 for the inputs:

Enter two integers, one for feet and one for inches: 1 1

The numbers you entered are 1 for feet and -858993460 for inches.
The total number of inches = -858993448
The total number of centimteres = -2.18184e+009
Press any key to continue

I also get this warning when I compile it:
d:\program files\microsoft visual studio\myprojects\test2\test2.cpp(28) : warning C4700: local variable 'inches' used without having been initialized


Thank you!

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |