• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

very noob c++ question

KAMAZON

Golden Member
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!
 
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;
}
 
Sir, I don't care what anyone says about you. You are a gentleman and a scholar, thank you very much! 😉
 
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.
 
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!

 
Back
Top