console calculator issues

Hello,

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>

using namespace 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;
}


Thanks for the help!
Well, what is it actually doing?

Also don't use globals unless you need to, its not good practice. And, I know all the tutorials have using namespace std; Try to avoid writing this line.

Finally, thank you for not using system("pause");
Why don't you want to have using namespace std? I have always used it, just wondering.
Well, I enter the integer for the length and hit enter. And then it asks for the width, but then there's a zero where I'm supposed to enter the width.

And also, what are globals. Sorry, I'm really new.
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?

Anyway, your code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace 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.
Last edited on
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!
using namespace 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.
I used(/use) the tutorial 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.

I think thats a justifiable use anyway.
Thanks for the help everyone. Just one quick question: what does the cin.ignore() statement do?
It ignores 1 character from cin I think (usually a '\n' if you just used >>).
@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.
Topic archived. No new replies allowed.