How to get rid of this error? [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 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.

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.

money converter c++ gives bracket errors [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 1 year ago.
Improve this question
Here is my code:
#include"std_lib_facilities.h"
int main()
{
cout << "Which currency do you wanna exchange for, $? €? or ¥?:\n";
string currency;
string dollars;
string euros;
string yen;
char $;
char €;
char ¥;
if (cin >> $);
cout << "You have chosen dollars.\n";
cout << "Please enter your amount:\n";
int amount;
cin >> amount;
cout << "Your amount is:" << amount*153 << "PKR" << endl;
keep_window_open();
return 0;
}
I tried making a money converter but my code till dollar wont run so I haven't wrote euro or yen yet. thank you for the response.
First of all, don't use those symbols as variable names, since they're not ASCII characters and could get messed up.
Secondly, if (cin >> $); doesn't do what you think it does. All it does reads something into the $ variable, and moves on regardless of what cin actually read because the if statement is terminated by a semicolon. The code after that is going to get executed, regardless of what cin actually read. Instead, you should do
if (condition) {
thing to execute if condition is true
}
Note the use of curly braces.
Thirdly, this is not how you check if the input string matches the dollar sign. What you should do instead, is read the input into a variable, and then compare that variable to the currency symbols.

Small syntax error with miles-to-kilograms converter exercise [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 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.

Program stops without running the while loop [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 4 years ago.
Improve this question
My while loop is not working. The code runs correctly on the Codecademy website compiler. I then compile it with Visual Studio, run it from the Command prompt and input a number. The program stops prematurely even if the number is the correct one.
#include <cstdlib>
#include <iostream>
int main() {
int answer = 8;
int guess;
int tries;
std::cout << "I have a number between 1-10.\n";
std::cout << "Please guess it: ";
std::cin >> guess;
while (guess != 8 && tries < 50) {
std::cout << "Wrong guess, try again: ";
std::cin >> guess;
tries++;
}
if (guess == 8) {
std::cout << "You got it!\n";
}
}
As #rsjaffe and #Ken White have said in the comments, the tries variable is unitiailized, meaning that the location in memory that the variable is pointing to is "junk" (left over memory). Try to give it an initial value, like this:
int tries = 0;
which will instantiate and initialize the tries variable.

for loop result 0 in 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 5 years ago.
Improve this question
I am a newbie in C++, I started to learn coding in C++ two weeks ago. Why does my code below always give me result 0 when I build and run? Please help
# include <iostream>
# include <string>
using namespace std;
int main ()
{
int input = 1;
cout << "input your number : \n";
cin >> input;
int faktorial = 1;
for(int i=1;i<=input;i++)
{
faktorial = faktorial * i;
}
cout << "factorial value from number " << input << " is " << faktorial << endl;
}
Your code works: https://ideone.com/CYFaxo
I suspect your problem is, you are looking at program exit code. When you don't return any value from main, program exit code is 0 (this is special case, and only non-void function where you may leave the return statement out), which conventionally means success (non-zero exit code usually indicates some kind of error, by convention).
Try to find the program output from your IDE, it should have the correct printout.