We're working with file streaming, and reading and writing to files. I managed to complete the assignment, but I feel there was probably a better way to have done it.. Here is the assignment my professor gave us:
Prompt the user to determine if they know their weight in kilograms. If they know the weight(answering yes to the question), the program will read a file containing the weight in kilograms and convert it to pounds. If they don't know the weight(answering no to the question), the program will prompt them to enter their weight in pounds and convert it to kilograms and store it in the file.
Thanks. Yeah it's not a very detailed program so there aren't too many ways to do it I guess. I was wandering if I should have used an else statement instead of while
just because it confuses you doesnt make it bad. its not badly or poorly written if its in a different style. his style is valid
Remember on all your life that code that confuses readers is a bad written code. It is not an "other style". It is simply a bad code. And who does not understand this is a simply a very bad programmer.
Remember on all your life that code that confuses readers is a bad written code. It is not an "other style". It is simply a bad code. And who does not understand this is a simply a very bad programmer.
The program does exactly what it's supposed to if I'm not mistaken.. I've been learning c++ for maybe a month and just started taking this class, which is why I posted in the beginner section! Either way, you fuel me to excel.
thats not true. if i show a beginner to c++ the stroustrop calculator and they are confused by it that doesnt make it badly written. it means they dont understand it properly
if ( /*...*/ ) {
} while ( /*...*/ ) {
}
is a valid style. its not poorly written. it is an other style.
oh no i understand it fine, but you might need to google some things if you think any style besides your own is poorly written. it doesnt confuse everyone, just you. i understood it just fine, because as i said its a style (or a variation of it) that is used by many people, namely brian kerningham and dennis ritchie.
1 - When reading in input use std::string instead of char. By using char, the program will crash if the user types more than one character for the answer. Nothing else in the code would need to be changed, by switching to string.
2 - Yes, an if else statement would have worked better than a while(). Especially because userAnswer never changes inside the loop, therefore the loop will never terminate.
3 - When opening files, check to make sure the file opened successfully. Streams are able to open a file during construction:
1 2 3 4 5 6
ifstream iFile("data_file.txt");
if( !iFile)
{
cout << "Error: Could not open input file" << endl;
return 0;
}
3 - close() does not need to be called on the files because the ifstream and ostream destructors automatically do that for you.
LOL great question man that needs to be taken out.. and do I need iomanip either? I think I just copy and pasted those includes before I did anything lol
nkendra gives good advice, but to add a bit on "Streams are able to open a file during construction", your other variables are also created long before they can be used.
For example, kilograms and pounds are constructed at line 13, but only first used at lines 24 and 25. In a trivial program such as this, it's easy to look up from line 24 to line 13 to find out what kind of variable it is, but as programs grow, it becomes harder.
The general rule is to declare variables as locally as possible, best of all only when they can be initialized meaningfully (this is item 18 in the C++ Coding Standards by Sutter/Alexandrescu)