Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

A little basic c++ help


ghost's Avatar
0 0

I'm written this little bit for CSCI151 and am having a bit of trouble. I've already finished the assignment using a different method so I'm not in any hurry, but why is this giving me the terrible and mathematically incorrect outputs that it's giving me?

Also, shouldn't cin.get() be waiting for input from the user before exiting the program?

// CSCI 151 Height Conversion Program
// G++ 4:4.3.3-1ubuntu1



#include <iostream>
using namespace std;

int main() {

string name;
int feet, inch;
int feet2 = feet*12;
int total_inch = (feet*12+inch);
int totalwtf = feet+inch;
double to_centimeter = ((total_inch*12+inch)*2.54);
double to_meter = (((total_inch*12+inch)*2.54)/100);


cout << "Hi, what's your name?: ";
getline (cin, name);
cout << "How many feet tall are you, " << name << "?: ";
cin >> feet;
cout << feet << " feet and how many inches?: ";
cin >> inch;
cout << "\n" << feet << " feet " << inch << " inches is equivalent to \n";
cout << total_inch << " inches or\n" << to_centimeter << " centimeters or\n";
cout << to_meter << " meters. \n\nPress return to exit.\n";
cin.get();
return(0);
}``` 

```markupHi, what's your name?: ted bundy
How many feet tall are you, ted bundy?: 5
5 feet and how many inches?: 6

5 feet 6 inches is equivalent to 
-1949649072 inches or
-4.53755e+09 centimeters or
-4.53755e+07 meters. 

Press return to exit.

ghost's Avatar
0 0

It gives you a bunch of seemingly incorrect data, because you make the calculations before you've put a value into them. Which means that it'll use any garbage data that's been left at the place that it assigns the variables to. You can't make formulas beforehand like that and assume that the script will automatically apply it to any value you later try to assign to it, for that you'd have to make functions, but let's forget that for now.

Furthermore, cin.get() will wait for input, yes. However, when you use cin and similar things to read in, then the return value will be stuck in the in buffer, which means that when something like cin.get() comes along that is perfectly happy with just reading in a return value, it will consume the return value in the in buffer and move on. Basically: if you know there'll be a return value in your in buffer, use two cin.get(); one for eating the lingering value and one to wait for input, thus making it pause.


ghost's Avatar
0 0

Ohhhh, thanks a bunch.


ghost's Avatar
0 0

Sqwertle wrote: I've already finished the assignment

I had already finished it by adding the arithmetic to the cout statements rather than in the declaration, and replaced cin.get() with system("pause"} as the program will be graded on a Windows platform. As far as course material, we weren't assigned books for the course but rather online notes, which don't cover anything further than what this programs level is, as they haven't been uploaded. Regardless, the instructors notes that have been uploaded do not cover trash issues. Being used to java, I wasn't expecting a declaration error due to a trash problem, and being that the course is using solely Windows platforms, the instructor will not be covering problems with the cin.get() statement. I don't appreciate being accused of plagiarizing my assignments.


ghost's Avatar
0 0

Sqwertle wrote: Regardless, the instructors notes that have been uploaded do not cover trash issues. Being used to java, I wasn't expecting a declaration error due to a trash problem. What is a "trash problem"?


ghost's Avatar
0 0

Java doesn't use garbage values that are left in the memory, C++ does. While that isn't necessarily a problem, it was the fundamental problem of the code I posted.


ghost's Avatar
0 0

Sqwertle wrote: Java doesn't use garbage values that are left in the memory, C++ does. While that isn't necessarily a problem, it was the fundamental problem of the code I posted. Java doesn't use garbage data because it has built in garbage collection. That wasn't what caused your error, just what caused strange values you got as a result. It's important that you know the reason for your error. Your problem was that you were trying to use variables that hadn't been initialized in an expression. Even if there had been no "garbage data" at those addresses, you still would have had an error. Same as Java.


ghost's Avatar
0 0

You should always initialize variables, especially pointers, in C++ or you may get some serious debugging to do. If you know for certain that it will get a valid value you may skip initialization, but if you're uncertain, be on the safe side. In Java all new variables are initialized to 0 or null depending on the type, so you don't get any garbage from the memory in them.


ghost's Avatar
0 0

Correct me if I'm wrong, but doesn't java only assign 0 and null values in the main part of a program (and possibly for object values when created), but will still not autoassign in lesser functions? Furthermore, as said, java doesn't exactly allow you to declare a variable in the way that has been done either where the value expected to be calculated would just be automatically calculated when assigned to it according to a formula. So any problem with this script would've persisted in java as well. As for the cin.get() "issue", cut the guy some slack, not even most books or teachers tend to explain what's wrong and why with it. It's a very common beginner's issue that people tend to ignore so that students turn to things like system("pause") instead, unfortunately. Also, Sqwertle, I chose you! Fuck that lil plantbacked hippie git and his flaming pet dragon!