Small syntax error with miles-to-kilograms converter exercise [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to get some practice in before my Intro to C++ class begins this Fall. I was going through some exercises in my textbook and I'm stuck on a miles-to-kilograms conversion exercise. Aparently my compiler says that it's expecting a ';' before line 7 but I don't understand where a ';' could possibly be placed before line 7?
#include <iostream>
using namespace std;
int main()
{
double miles;
double kilograms == miles * 1.609;
cout << "How many miles away is your destination? ";
cin >> miles;
cout << "Your destination is " << kilograms << " kilograms away!";
}

double kilograms == miles * 1.609; should be double kilograms = miles * 1.609; as == is used for comparisons / conditional statements.
Another problem, you should place kilograms = miles * 1.609; right after cin >> miles; because right now, it does the calculation on an empty variable and will likely create another error later, or just return 0.
Sidenote: the person who made the question confused kilograms for kilometres, so just ignore that.
EDIT: On the using uninitialized variable..., make sure to change double miles; to double miles = 0; as C++ (and many more languages) requires variables to be assigned before they're accessed.

Related

I cant seem to get the amount of decimals right using setprecision c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I'm currently getting into subroutines/subprograms or whatever you call them in english and in this specific assignment that i'm trying to solve I have to calculate the average length of two words.
void Length(string const text_1,
string const text_2,
int & total_length,
double & mean_length)
{
total_length = text_1.length() + text_2.length();
mean_length = static_cast<double>(total_length) / 2;
}
void Length_Program(int val)
{
string text_1;
string text_2;
int total_length{};
double mean_length{};
cout << "Mata in två ord: ";
cin >> text_1 >> text_2;
cout << "Totallängd: ";
Length(text_1, text_2, total_length, mean_length);
cout << total_length << endl;
cout << "Medellängd: " << fixed << setprecision(1) << mean_length;
}
I have set the precision to setprecision(1) and I assume it will only write one decimal but I keep getting two decimals.
my example is: abcd E
it should say that it is an average of 2.5 words but it says 2.51 for some reason. Can someone help me understand what i'm doing wrong?
Your problem is that you forgot << endl on your last output line. The return code shown by the OS is appended to your output. The setprecision is working just fine.

Why is the int function acting strange when I set it to 500? c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I am trying to set a int to 500, but random numbers such as 22067 come out.
I am trying to make a simple gambling game. Currently, what I'm doing is that I set int gambledMoney = 500; But when I ask to print the 500, it does work but instead it prints 22067. Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//introduction
cout << "Why hello human!\nWelcome to this gambling game where you... gamble!\nTo explain how to play here are the steps:\n\n";
//instructuions
cout << "You always start with $2000.\nAt the very beginning of each new round, you will be given choices on the amount of money you will gamble.\n";
cout << "Then you will gamble against an AI.\nIf you win, you gain the amount that the AI gambled...\nBut if you lose, you lose the money that you gambled.\n";
cout << "If you reach $500, you lose. Same goes with the AI.\n";
//game start
cout << "\nNow lets start!\n";
//gamble amount
string gambleChoice;
int gambledMoney;
cout << "\nHow much would you like to gamble?";
cout << "\n A) $500\n B) $750\n C) $1000\n D) $1250\n E) $1500\n F) $1750\n G) $2000\n\n";
//amount chosen
cin >> gambleChoice;
if (gambleChoice == "a")
{
int gambledMoney = 500;
}
cout << "\nYou have gambled $" << gambledMoney << "!" << endl;
return 0;
}
Does anyone know why it is not putting 500?
You are declaring two different variables with the name gambledMoney in different scopes, so that one variable "shadows" the other variable with the same name.
The line
int gambledMoney = 500;
will create a new variable with that name, and set it to 500. It won't change the value of the existing variable to 500.
You probably want to change that line to the following:
gambledMoney = 500;
That way, it will change the value of the existing variable, instead of creating a new one.
If you are using the compilers gcc or clang, I recommend that you compile with the -Wshadow command-line option. That way, the compiler will warn you when you create two variables of the same name, so that one shadows the other.
That is not the only compiler warning that I recommend enabling, though. You may want to read this for further information:
Why should I always enable compiler warnings?

For loop displaying incorrect information stored inside array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this really simple program, with a for loop designed to run 3 times.
For every iteration, it will ask the user to enter in the weather. Each time, the user's input is stored in an integer array called C.
Once the loop concludes, I have another for loop that print's out the user's input. This next loop works fine for the 2 values, but gives off some weird messed up value once it reaches the third iteration.
int main(){
//Variable declaration:
int days;
int C[2];
int F[2];
for(days=0; days<3; days++){
cout << "What is the temperature in celsius for day " << days + 1 << ":" << endl;
cin >> C[days];
F[days] = (C[days] * 9/5) + 32;
}
cout << "\nCelsius\t\t Farenheit\n-------\t\t ---------" << endl;
for(days =0; days <3; days++){
cout << C[days] << "\t\t " << F[days] << endl;
}
return 0;
}
This next loop works fine for the 2 values but gives off some weird messed up value once it reaches the third iteration.
Your definition of C states it has 2 entries, but your loop runs for
three iterations (0, 1, and 2).

Getting wrong output in any math operation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
#include
#include
using namespace std;
int main()
{
int x ;
int y ;
int sum ;
sum = x-y ;
cout << "Enter your first number here: ";
cin >> x ;
cout << "Enter your second one : ";
cin >> y;
cout << "your result is " << sum << endl;
system("pause") ;
}
Now when i run this program:
Enter your first number here: 88
Enter your second one : 22
your result is 2221280 .
i've searched a lot but don't know what to do .
C++ arithmetic on builtin types isn't symbolic (like syms library in MATLAB if you're familiar with that). It's numeric. sum = x - y doesn't establish a relationship that causes sum to be updated whenever x or y are updated. It takes the value of x and y at that moment, executes the operation, and assigns it to sum. The relationship between sum, x, and y is ended after this transaction.
As an aside this should be called difference not sum, since you're subtracting.

How to get rid of this error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I was assigned to do a simple number converter, I followed all the instructions and they seemed pretty straight forward. This is for a beginner course into C++ and I seem to be missing the mark on this program. I continue to get an error stating that I need an initializer before double inputedNumber or that the variable is not in the scope. I have even compared my code to classmates and did what they did but this error still happens...
Any help would be awesome!
Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
double inputedNumber;
cout << "Please input a decimal to be converted.";
cin >> inputedNumber;
cin >> "Number Converter!! The given number is" >> inputedNumber;
}
cin >> "Number Converter!! The given number is" >> inputedNumber;
does not compile, since cin is only for input not output; use cout instead!
cout << "Number Converter!! The given number is" << inputedNumber;
Also, if you really want to convert a number (to an integer for example) use this:
cout << "Number Converter!! The given number is" << static_cast<int>(inputedNumber);
First Line in main function is missing a semicolon.