C++ help

A named constant is a value that cannot change throughout the course of the program, like, for example:
Code:
const double PI = 3.14159;
//or
const int LENGTH_OF_DAY_IN_HOURS = 24

following my previous example, this code would generate an error:

Code:
const int MY_CONSTANT_INT = 15;
//some code here.
MY_CONSTANT_INT = 7;
That will generate an error because you cannot re-assign a value to a constant.

As it stands, writing 'centimeters = inches*2.54' will work, as long as 'inches' has already been given a value. You will have to rewrite that line every time you want to give 'centimeters' a new value, though, so what I would do is put your conversion into a function, like this:

Code:
double toCentimeter(double inches){
return inches*2.54;
}

int main(){
//some code here.
int inches = 12;
double centimeter = toCentimeter(inches);
}
That way, whenever you want to update the value of 'centimeter', you just need to set it equal to the function 'toCentimeter', with the length in inches (either as a numeric value, or as a variable) as your argument.
 
Last edited:
This is what I put and it works but it wanted me to put the value 2.54 for centimeters and feet by 12 for inches into named constants...do you think i would get points taken away for the way i have it.....(stupid question)


Code:
int feet;
int inches;
int centimeters;

cout << "Please enter the amount of feet and inches." << endl;
cout << "feet->";
cin >> feet;
cout << "inches->";
cin >> inches;
inches = feet * 12+inches;

cout<< "Total amount in inches:" << inches << endl;
centimeters = inches*2.54;
cout << "Total amount in centimeters:" << centimeters << endl;
 
This is what I put and it works but it wanted me to put the value 2.54 for centimeters and feet by 12 for inches into named constants...do you think i would get points taken away for the way i have it.....(stupid question)
That depends on what the point of the assignment was...and if your teacher/professor wanted you to add constants to your program. That's not up to us.

You could make the 2.54 into a constant and make the 12 into a constant as well if you wanted to. Not going to hurt or help anything unless you're supposed to for your assignment.
 
Error c4700 for a,b,andc....wouldnt the sum define a,b,c??

Code:
#include<iostream>
using namespace std;
int main()
{
    double a;
    double b;
    double c;
    double sum;
    sum=a + b + c;
    cout<< "Enter three double values separated by a space:";
    cin >> sum;

    system ("pause");
    return 0;
}
 
To be blunt...that's not correct at all.

You want to read in (using cin >>) values into the 3 values. Then do "sum = a + b + c;" and then you can output/use the sum variable.

You're declaring empty (NULL) variables, trying to add those NULLs (which WON'T work), and then you're (for some reason) having the user input a value for "sum" ?
 
It works until I put in the formula....AHHHHHHH

Code:
#include<iostream>
using namespace std;
int main()
{
    double a;
    double b;
    double c;
    double sum;
    cout<< "Enter three double values separated by a space to find the sum.:";
    cin >> a,b,c;
    sum = a + b + c;
    cout << endl;
    cout << "The sum is";
    cin >> sum;
    system ("pause");
    return 0;
}
 
Change
Code:
cin >> a,b,c;
to
Code:
cin >> a >> b >> c;

the ">>" operator is called the "insertion operator", by the way. If you have several consecutive inputs that you need to get from the user and store in separate variables, you can use multiple insertion operators to tell your compiler in what order the variables you wrote will get the input.

In my above example, cin will store the information preceding a new-line character into variable 'a', the next line will be stored in variable 'b', and the last line will be stored in variable 'c'.

Writing
Code:
double a, b, c, shabaLabaDingDong;
cin >> a >> b >> c >> shabaLabaDingDong;

is the equivalent of writing
Code:
double a, b, c, shabaLabaDingDong;
cin >> a;
cin >> b;
cin >> c;
cin >> shabaLabaDingDong;
Because of that, though, you'll have to enter the data like this:

12.5 [enter]
16.88 [enter]
.225 [enter]
 
Last edited:
It works until I put in the formula....AHHHHHHH

Code:
#include<iostream>
using namespace std;
int main()
{
    double a;
    double b;
    double c;
    double sum;
    cout<< "Enter three double values separated by a space to find the sum.:";
    cin >> a,b,c;
    sum = a + b + c;
    cout << endl;
    cout << "The sum is";
    cin >> sum;
    system ("pause");
    return 0;
}
You're also trying to output "sum" incorrectly. You're reading a value in from the user after you've already computed the value for sum, which overwrites the computed value. You'd want to change that to a cout instead of having a cin.

Change
Code:
cin >> a,b,c;
to
Code:
cin >> a >> b >> c;

Try not to give the answer directly or without explanation. Assuming this is homework, since OP had posted a previous thread about a C++ assignment.
 
You're also trying to output "sum" incorrectly. You're reading a value in from the user after you've already computed the value for sum, which overwrites the computed value. You'd want to change that to a cout instead of having a cin.



Try not to give the answer directly or without explanation. Assuming this is homework, since OP had posted a previous thread about a C++ assignment.

Wasn't aware of a previous post. Woops. :(
 
Back
Top Bottom