Solution to set precision errors [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
Where am I going wrong? I inclduded setprecision(2) but the output comes wrong.
example: if the sum is 23 and n is 10, the answer is 2.00
for(i=3; i<n; i++)
{
sum=sum+marks[i];
}
cout<<"Total marks of the student is "<<sum<<endl;
avg=sum/n;
cout.setf(ios::showpoint);
cout<<setprecision(2)<<fixed;
cout<<"Average marks of the student is "<<avg<<endl;
getch();

That's because you're using the integer division, in which the fractional part (remainder) is discarded.
Change
avg=sum/n;
to
avg=sum/(float)n;

Related

C++ if statement/xcode [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
For some reason, my "if" expression will not work in Xcode. It gives a "parse issue" with the if expression. Any suggestions? The error code is: "Expected unqualified-id". I am working with xcode. Please see below:
if (surfaceArea > 750)
{
totalCost t=50;
}
Thanks in advance,
if (surfaceArea > 750) //adding 50 dollars to cost if product is
//over 750 sq. inches
{
totalCost t+=50;
}
make a comment line // .
Hope you understood.
To write a multi line comment use /* .... */
Just remove over 750 sq. inches from your code. And use /* */ for multiline comments.

My linked list is not working correctly [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
following is my code for the program that will print out the random position contained values of nodes of a linked list. The problem is that my list is not printing the complete result. It prints only one result and thus. Please tell me where i am wrong.
int main(){
List* n;
int value=3;
int *counter=0;
collect(value,counter);
for(int i=0; i<&counter; i++);
{
count<<"\n Shuffled: "<< n.pickanddestroy();
}
}
Remove the semicolon at the end of the first line of your for loop.

C++ character check [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have just started to learn C++ and i would like to get some help.
The user needs to type an ID number and the format has to be the following. The first character B and the other 4 any integer.
Im trying to check if the character format are right.
So far i have this:
if ((isalpha(id[0])=='B' ) && (isdigit(id.at(1))) && (isdigit(id.at(2))) ......
{
//do something
}
else
{
cout << "Wrong format" << endl;
}
but even if i type example B8745 it says wrong format.
You are comparing the result of isalpha, which is boolean, to character literal 'B'.

Read from file negative numbers 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 7 years ago.
Improve this question
I have to get numbers from file and some of them are negative. So, how can I do that? (C++)
Now I'm trying just this (without something special):
U1.txt file:
4 5 -1 6 -2
My code:
ifstream fd(FD);
int n1,n2,n3,n4,n5;
fd>>n1>>n2>>n3>>n4>>n5;
Your code is basically correct, except for that typo with n4 missing.
This:
ifstream fd("U1.txt");
int n1,n2,n3,n4,n5;
fd>>n1>>n2>>n3>>n4>>n5;
Will do what you expect, modulo error conditions.

a Sequential that decreased by 5 using a for 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 8 years ago.
Improve this question
i want to print a Sequential that decreased by 5
using a for loop
but i don't know why it doesn't print the output :(
#include<bits/stdc++.h>
using namespace std;
int main(){
for(int j=60;j<=0;j-=5){
cout<<j<<endl;
}
}
whats the wrong?!
Change it to
for(int j=60;j>=0;j-=5){
^^