Guys please help me again with my program. I changed the order of codes in it. Please check what is wrong with my codes. It runs but it doesn't perform the task it should do. It should compute the total of the grades inputted by the user and show the corresponding remarks. Unfortunately, it doesn't work :( please help me
#include<iostream>
#include<conio.h>
using namespace std;
void computePG(int& PG);
void Remark(int PG);
int x, y, z, w, p;
int prelimGrade,yourRemark,PG;
int preliminaryGrade;
int main()
{
int pGrade;
cout<<"Programmed by: Katrina G. Gozo, 1ISC";
cout<<endl<<"\nDate: Aug. 23,2013";
cout<<endl<<"\nThis program intends to compute the PG and make the necessary remarks";
cout<<"\n\nPlease enter your score on quiz 1 ";
cin>>x;
cout<<"\nPlease enter your score on quiz 2 ";
cin>>y;
cout<<"\nPlease enter your score on quiz 3 ";
cin>>z;
cout<<"\nPlease enter your score on prelims ";
cin>>p;
computePG(pGrade);
Remark(pGrade);
getch();
}
void computePG(int& PG)
{
PG = x/30 * 20 + y/50 * 20 + z/40 * 20 + w/100 * 40;
cout << "\nYour prelim grade is " << PG;
}
void Remark(int PG)
{
if (PG>=90)
cout<<"A." <<endl;
else if (PG>=80)
cout<<"B."<<endl;
else if (PG>=70)
cout<<"C."<<endl;
else if (PG>=60)
cout<<"D."<<endl;
else
cout<<"E."<<endl;
}
You're most likely running afoul of integer arithmetic. Note: when dividing an integer by another integer, you get an integer result (rounded towards zero).
So you'll want to use double as the type of PG and pGrade, and make the constants in computePG floating points numbers as well, by writing them as 30.0, 20.0, etc.
You should probably use a double for your variable "PG," that way you'll have adequate decimal accuracy.
Also, you may want to avoid global variables in the future, because I'm guessing it's how you made this error - you never assign a value to w before using it, which means it is assigned the value 0 by the compiler and may be what is screwing up your result.
Related
I'm getting a compiler error when trying to build the code below and I don't quite understand what is failing and why.
It's pointing to line 45 (cin>>x[ctr]) with a "no type named 'type' in 'struct std::enable_if<false, std::basic_istream&>'" message.
I've just started coding a few days ago and English is not my native language. Apologies if this question is below the community's paygrade. Hope you can point me in the right direction.
cpp.sh/34sm3
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
//#include "_pause.h"
using namespace std;
//////////////////////////////////////////////////////////////////
// NOTE
// This is your program entry point. Your main logic is placed
// inside this function. You may add your functions before this
// "main()", or after this "main()" provided you added reference
// before this "main()" function.
//////////////////////////////////////////////////////////////////
//Write a program that can divide six non-zero integers (two integers per division) from the user and display the result to the user.
//Create a function that will perform the division operation. Display only the non-decimal part of the quotient.
float quot (int num1, int num2)
{
return num1/num2;
}
int main()
{
// ************************** TO DO **************************
// Place your code logic after this comment line
// ***********************************************************
int x[6];
cout<<"Enter 6 integers to divide: ";
for(int ctr=0; ctr<6; ctr++)
{
cin>>[ctr];
if(x[ctr]==0)
{
cout<<"Invalid number! Please enter a non-zero value: ";
cin>>x[ctr];
}
}
cout<<"The quotient of the first pair is: " << quot(x[0],x[1]) <<endl;
cout<<"The quotient of the second pair is: " << quot(x[2],x[3]) <<endl;
cout<<"The quotient of the third pair is: " << quot(x[4],x[5]) <<endl;
system ("pause");
return EXIT_SUCCESS;
}
Are cin statements inside for loops not allowed in C++?
i guess you need to replace cin>>[ctr] with cin>>x[ctr].....this might fix your error.
For starters the function quot should be written like
float quot (int num1, int num2)
{
return static_cast<float>( num1 ) / num2;
}
Otherwise the return type float does not make a great sense because in this expression num1 / num2 there is used the integer arithmetic.
It is obvious that in this statement
cin>>[ctr];
there is a typo. You forgot to specify the array name x
cin >> x[ctr];
Also it will be better to subsritute this if statement
if(x[ctr]==0)
{
cout<<"Invalid number! Please enter a non-zero value: ";
cin>>x[ctr];
}
for a while statement like
while ( x[ctr] == 0 )
{
cout<<"Invalid number! Please enter a non-zero value: ";
cin>>x[ctr];
}
That's all you had to do.
Just change cin>>[ctr] to cin>>x[ctr]
It's on line 41. You need to specify the array name, x before the index [ctr]
Here's your code.
I recommend you to use a debugger to figure out such small human-made-errors yourself.
Also, read your code and try to visualize its flow before asking for solutions.
int main()
{
// ************************** TO DO **************************
// Place your code logic after this comment line
// ***********************************************************
int x[6];
cout<<"Enter 6 integers to divide: ";
for(int ctr=0; ctr<6; ctr++)
{
cin>>x[ctr];
if(x[ctr]==0)
{
cout<<"Invalid number! Please enter a non-zero value: ";
cin>>x[ctr];
}
}
cout<<"The quotient of the first pair is: " << quot(x[0],x[1]) <<endl;
cout<<"The quotient of the second pair is: " << quot(x[2],x[3]) <<endl;
cout<<"The quotient of the third pair is: " << quot(x[4],x[5]) <<endl;
system ("pause");
return EXIT_SUCCESS;
}
I have been trying to write a simple code in C++ to calculate CGPA. It is a practice code. There is no error in code when I start to build. But it is not running.
I am using codeblocks. I have checked everything. but can not find any problems in it.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int g,h,i,a=0, grade[g],hour[h];
char course[10];
float z=0,sum =0, result=0, totalhour=0;
cout<<"How Many Course's do you want to calculate for CGPA: ";
cin>> a;
cout<<endl;
cout<<"Please Enter your Course name, Credit hour & Grade point of the your course"<<endl;
for(i=1;i<=a;i++)
{
cout<<i<<". no Course name: ";
cin>>course[i];
cout<<"-----";
cout<<"Credit Hour: ";
cin>>hour[h];
cout<<"-----";
cout<<"Grade point: ";
cin>>grade[g];
cout<<"\n";
}
for (i=1; i<=a;i++)
{
cout<<i<<".no Course----";
z= grade[g]*hour[h];
cout<<"Grade point X Credit Hour = "<<grade[g]<<" X "<<hour[h]<<" = "<<z<<endl;
sum = sum+z;
totalhour= totalhour+hour[h];
}
result = sum / totalhour;
cout<<"Your total Credit hour Completed is : "<<totalhour<<endl;
cout<<"----Your Total CGPA is -------- = "<<result<<endl;
getch();
return 0;
}
You have some problems in your code:
What happens if user input is larger then 10? you assume it will be smaller (you will get undefined behavior since the array size is 10, and your for loop runs until user input)
int g,h,i,a=0, grade[g],hour[h]; - what is the size of grade and hour? you can not "dynamically" give them a size. You must tell what size they are when you declare them, unless you use dynamic containers such as std::vector.
I'm new to c++ so what I like to do is make numerous calculators of anything, varying from area calculators to quadradic formula and etc. Anyways I'm creating a triangle area calculator but there is one small problem but first here's my code:
#include <iostream>
#include <cmath>
using namespace std;
int triangle()
{
int base;
int height;
int area;
cout << "Enter base: ";
cin >> base;
cout << "Enter height: ";
cin >> height;
area = base * height /2;
cout << area;
return area;
}
int main ()
{
cout << "Formula Calculator \n";
cout << triangle();
return 0;
}
The input is this:
Enter base: (I enter 4)
Enter height: (I enter 5)
1010
As you can see the 5 * 4 * 1/2 is not 10 for some reason every number that the area is, is always digit doubled if you know what I mean for ex if the area is 20 the program will show 2020, please help.
You output the area twice.
cout << area; // here's once
cout << triangle(); // here's twice
I would suggest rethinking your code. Should the triangle function just compute the area or should it ask for input and do output? If the latter, why cout << triangle();?
When you are inside your function triangle(), check it's 2nd last line, It is cout<<area, let's say you have given input 4 and 5 so inside the body of the function, at cout<<area it will output the area. Now it returns area which is then outputted by 2nd last line of your program i.e cout<< traingle()//return value is outputted so that's why we are seeing 2 outputs. It is not only in the case of 4 and 5, but it will also output area 2 times in every case.
I've been recycling an assignment to further practice and develop my programming skills in my class and I'm having an issue with 3 DO WHILE loops within another Do While loop.
I'm trying to deny Test scores for test1, 2 and 3 that are less than 1 and greater than 100.
I'm encountering that the loops are not processing what I am inputting for Test1/2/3. It's allowing values out of the while range to pass through. Is there anyone who can suggest or see what I might be doing wrong? Thanks ahead of time guys!
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double computeavg (int a, int b, int c);
char lettergrade (double z);
int main ()
{
double test1, test2, test3, average; //test1/2/3, test scores, Average: average of test scores
double tottest1=0, tottest2=0, tottest3=0, avg1, avg2, avg3; //tottest# - sum of test grades collected for that number test, avg# average for first second or third test
int student=0, avgvar; //average variable, Student number
char grade, ans; // Holds a letter grade, holds a response to a question
do{
student=student+1;
cout<<"Hello Student #"<<student<<endl;
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1>=1||test1<=100);
do{
cout<<"Please input test 2 Grade ";
cin>> test2;
}
while(test2>=1||test2<=100);
do{
cout<<"Please input test 3 Grade ";
cin>> test3;
}
while(test3>=1||test3<=100);
average=computeavg (test1, test2, test3);
cout<<setprecision(0)<<fixed;
cout<<"Your Average is: "<<average<<endl;
tottest1=tottest1+test1;
tottest2=tottest2+test2;
tottest3=tottest3+test3;
grade = lettergrade(average);
cout << "Your grade is " << grade << endl;
cout<<"Do you want to grade another student? (y/n)";
cin>>ans;
cout<<"\n";
} while(ans=='y');
I think you need to change do while as below:
do{
cout<<"Please input test 1 Grade ";
cin>> test1;
}
while(test1<1||test1>100);
Your conditions are backward. Look carefully: while(test3>=1||test3<=100). All numbers satisfy at least a portion of the condition. For example, −4 is not greater than or equal to 1, but it is less than or equal to 100, and since your conditions are joined with the || operator, which is the or operator, the condition evaluates to true.
You need to change the condition. This is homework, so I won't tell you precisely what condition you should use instead.
You can use a debugger to discover this problem yourself. You can also discover it by carefully walking through your program by hand. Get a pencil and some paper, and write down the current values of your variables. Keep track of which instruction you're on, and then work your way through the program. When a variable changes, cross out its old value and write down the new one. Eventually, you'll be able to do that in your head, but while you're learning, it helps to just write it down.
My program is meant to ask a user to input an equation. Then find the maximum over an interval given by the user. When I compile my program, the output I get is:
Please complete the equation to be evaluated f(x)=
Please enter the first number of the interval to be checked:
Please enter the last number of the interval to be checked:
Please enter the desired initial step size:
sh: PAUSE: command not found
with the last line repeating many times.
I think the problem here has something to do with having the user input the equation to be tested. However, I'm unsure of how to fix this.
Here's my code
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
int a, b, delta, fx, x, y;
int max = 0;
cout <<"Please complete the equation to be evaluated f(x)= " << endl;
cin >> fx;
cout <<"Please enter the first number of the interval to be checked: " << endl;
cin >> a;
cout << "Please enter the last number of the interval to be checked: " << endl;
cin >> b;
cout << "Please enter the desired initial step size: " << endl;
cin >> delta;
for(x = a; x <= b; x = x+delta)
{
y = fx;
if (y > max)
{
max = y;
cout <<"The maximum over the interval from " << a <<"to " << b <<"is " << delta;
}
else
{
delta= delta/2;
}
if (delta < pow( 10, -6))
{
system ("PAUSE");
}
}
return 0;
}
F(x) shouldn't be an integer variable, it should be a string variable. That way, the user can enter operators as characters instead of the compiler thinking they should be numbers. You would then have to process the string to determine the equation; this would require some thought, and possibly a more advanced data structure such as a binary tree.
Simply don't use system("pause"); in the if statement and you'll lose that error:
"sh: PAUSE: command not found". Place it right before the end of the main.
system("pause");
return 0;
As pointed out by others, the form of f(x) could be an issue with the above code.
Consider to redesign what to achieve for your program. One possibility is to narrow down the f(x) as polynomial function so that you can avoid parsing general algebraic equation, in this case you can ask:
how many degree of the polynomial ? upon this, it is followed by input the coefficient value for each factor in the polynomial equation.
This way, you can still use integer (or double - better) in the program.