What's new

Help with basic C++ perimeter/area calculator

Maza

Maza

Video Editor
Messages
290
Reaction score
256
Points
295
Sin$
0
Hello all,

I have been learning C++ for about a week & I am trying to make a basic area & perimeter calculator where the user puts in the length and the width and the program will take care of the rest but I am having troubles on finding what is wrong, Maybe someone here can help ?

Also if you see anything wrong with the way I code please tell me, Still am extremely new and learning.

Code:
#include "stdafx.h"

#include <iostream>



using namespace std;

int main()
{

   
    int length;
    int width;
    int perimeter;
    int area;

    perimeter = 2 * (length + width);
    area = length * width;

    cout << "Welcome to the area and perimeter calculator\n";
    system("pause\n");
   
    cout << "Please enter the length\n";
    cin >> length;    // Where user enter's length
    system("pause\n");

    cout << "Please enter the width\n";
    cin >> width; // Where user enter's width
    system("pause\n");

    cout << "perimeter calculated successfully\n";
    system("pause\n");

    cout << "The perimeter is: /n";
    cout << perimeter << endl;      // permimeter gets displayed here
    system("pause\n");

    cout << "The area is: \n";     // area gets displayed here
    cout << area << endl;

    system("pause");
    return 0;

}
3lmmi1n.png
 
stepto

stepto

Enthusiast
Messages
249
Reaction score
72
Points
110
Sin$
0
How do I Initalize it ? I thought I did that when I said "Int length" & "Int width"?

halp
To initialize a variable, you have two options: implicit and explicit initialization.
Example on the two:
C:
//implicit
int length(3);
int width(3);

//explicit
int length = 3;
int width = 3;

//explicit v2 (preferred way)
int length{3};
int width{3};
What you were doing:
C:
int length;
int width;
This is called declaring a variable.

Also, the errors you're getting are because you never initialized length or width before declaring
C:
int area = length * width;
Therefore, after you assign an int to width (cin >> width), then you can declare area.

EDIT: Also btw, you shouldn't switch between std::endl and '/n'. Although the preferred method is using std::endl simply because it flushes the buffer.
 
Last edited:
Cakes

Cakes

お前はもう死んでいる
VIP
Retired
Mythical Veteran Platinum Record End of the Year 2017
Messages
20,705
Reaction score
20,272
Points
3,870
Sin$
-7
How do I Initalize it ? I thought I did that when I said "Int length" & "Int width"?

halp
The issue with your code are these two lines:
C:
perimeter = 2 * (length + width);
area = length * width;

When you created your variables, none of them were initialized; they have no value. There are multiple ways to fix this issue that you have. The simplest way to resolve your issue to move those two lines after you've taken the user input for length and width. The following code should work fine:
C:
#include "stdafx.h"

#include <iostream>
using namespace std;

int main()
{
    int length;
    int width;
    int perimeter;
    int area;

    cout << "Welcome to the area and perimeter calculator\n";
    system("pause\n");

    cout << "Please enter the length\n";
    cin >> length;    // Where user enter's length
    system("pause\n");

    cout << "Please enter the width\n";
    cin >> width; // Where user enter's width
    system("pause\n");

    //Calculates Perimeter and Area based on the user's inputs and assigns it to our variables
    perimeter = 2 * (length + width);
    area = length * width;

    cout << "perimeter calculated successfully\n";
    system("pause\n");

    cout << "The perimeter is: /n";
    cout << perimeter << endl;      // permimeter gets displayed here
    system("pause\n");

    cout << "The area is: \n";     // area gets displayed here
    cout << area << endl;

    system("pause");
    return 0;

}
 
stepto

stepto

Enthusiast
Messages
249
Reaction score
72
Points
110
Sin$
0
The issue with your code is these two lines:
C:
perimeter = 2 * (length + width);
area = length * width;

When you created your variables, none of them were initialized; they have no value. There's multiple ways to fix this issue that you have. The simplest way to resolve your issue to move those two lines after you've taken the user input for length and width. The following code should work fine:
C:
#include "stdafx.h"

#include <iostream>
using namespace std;

int main()
{
    int length;
    int width;
    int perimeter;
    int area;

    cout << "Welcome to the area and perimeter calculator\n";
    system("pause\n");
 
    cout << "Please enter the length\n";
    cin >> length;    // Where user enter's length
    system("pause\n");

    cout << "Please enter the width\n";
    cin >> width; // Where user enter's width
    system("pause\n");

    //Calculates Perimeter and Area based on the user's inputs and assigns it to our variables
    perimeter = 2 * (length + width);
    area = length * width;

    cout << "perimeter calculated successfully\n";
    system("pause\n");

    cout << "The perimeter is: /n";
    cout << perimeter << endl;      // permimeter gets displayed here
    system("pause\n");

    cout << "The area is: \n";     // area gets displayed here
    cout << area << endl;

    system("pause");
    return 0;

}
Beat you to it :wink:
 
WildeThing

WildeThing

Enthusiast
Messages
212
Reaction score
63
Points
130
Sin$
7
Try not use system("pause"); since it's windows specific and it's not good practice since, for console applications, it's the holy grail to be cross platform. Use std::cin.ignore().get(); You may as well also use std::endl in the streams rather than platform dependent (I know, ridiculous) new line escaped char.
 
Maza

Maza

Video Editor
Messages
290
Reaction score
256
Points
295
Sin$
0
Try not use system("pause"); since it's windows specific and it's not good practice since, for console applications, it's the holy grail to be cross platform. Use std::cin.ignore().get(); You may as well also use std::endl in the streams rather than platform dependent (I know, ridiculous) new line escaped char.
My professor told me to use that :frown:
 
WildeThing

WildeThing

Enthusiast
Messages
212
Reaction score
63
Points
130
Sin$
7
My professor told me to use that :frown:
Use it if you want but system() just executes like the terminal /bin/ executables in environment variables. Pause doesn't exist on Mac or Linux but std::cin.ignore().get() does since it's part of the standard.

You'll usually be taught std::endl before escaping characters.
 
Top Bottom
Login
Register