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.
Related
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.
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).
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.
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.
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 8 years ago.
Improve this question
As I understand from
Left shift Float type
one cannot use left shift operator on float values. But when I tried it, it gave the same answer as multiplying by 2n.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float a = 1.1234;
int b = (int)(a*(1<<10));
int c = (int)(a*pow(2,10));
cout << "\n a = " << a << " b = " << b << " c = " << c;
return 0;
}
It outputs a = 1.1234 b = 1150 c = 1150
In which case will the two outputs (b and c) differ?
int b = (int)(a*(1<<10));
Here, since both 1 and 10 are integers, you are performing left shift operation on integer instead of on floating-point number.
you are multiplying 1024 with value of a(1.1234) in both case.
it does not mean you are shifting float value.