I am very new to C++ (I've only been using it for several hours), and I need some help. I want the console app to calculate the area of a rectangle using integers entered by the user. For some reason, it just isn't working
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int length;
int width;
int area;
char hold;
int main()
{
cout << "Enter the following information to calculate the area of a rectangle.\n\n";
cout << "Length as integer:\t"; cin.get(length);
cout << "Width as integer:\t"; cin.get(width);
area = length * width;
cout << area;
cin.get(hold);
return 0;
}
Also don't use globals unless you need to, its not good practice. And, I know all the tutorials have usingnamespace std; Try to avoid writing this line.
That doesn't even compile for me. I'm not sure if you can even use cin.get() to get integer values.
Anyway, i'm not sure what resource you are using but I would change it ie. maybe to this sites tutorial? or a book?
#include <iostream>
usingnamespace std;
int main()
{
int length, width, area;
cout << "Enter the following information to calculate the area of a rectangle.\n\n";
cout << "Length as integer:\t";
cin >> length;
cin.ignore();
cout << "Width as integer:\t";
cin >> width;
cin.ignore();
area = length * width;
cout << area;
cin.get();
return 0;
}
Without being rude, to explain the changes I have made would be a waste of both our times. You need to find a new tutorial and re-learn from the beginning. But i promise you you will then understand why I have made them.
Okay, thanks, I'll give it a try. I didn't find a tutorial for this, I just kinda winged it, which i guess wasn't such a good idea. Thanks for the help!
usingnamespace std; is considered bad practice because of namespace pollution (e.g. you are basically putting all of namespace std into the global namespace, which can cause problems)
Anyway, if you haven't found a tutorial yet, try the one on this site.
Globals are variables that can be accessed by any function. So if you define a variable outside of a function; it can be used by all succeeding functions. I just use globals if I want to pass variables to a function that is called by another function -- to avoid passing variables through functions that don't use them, except to pass them as parameters to other fuctions. For example if my main function called another function, which called another function that called a final function which needed variables defined in main; rather than pass parameters unnecessarily through 2 functions that weren't going to use them; I could use globals instead. Or if I had two functions that needed the same parameters.
@chrisname, a better idea would be to create a namespace called global and put the globals there and then in the individual functions put using namespace global.
As firecraco said cin.ignore() ignores 1 specified characeter, or '\n' if no character is specified. Basically, when the program reaches cin>>length; and you input a number and press enter you are actually inputting a number and '\n'. So say you input the number 6, you are actually inputting 6\n. So you use cin.ignore() to ignore than newline character. Otherwise you program can go a bit tits up lol.