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.
Related
I'm currently working on an assignment and it's meant to take the concept of a car body shop offering different trim packages at different prices, the program is meant to use a pretest loop to stop the function if a user inputs a code that isn't already on the array, and then the user inputs the base price of their car and the program is meant to add the base price, the trim price and a 15% sales tax and give the new user their total cost. If I only had to create a function that displayed array's I think I'd have no trouble but I'm currently tearing my hair out trying to wrap my head around how to get all the different functions to work together
currently my algorithm is
1)enter the base price of car
2.) Enter the trim package code
3.) searchIndex=0
while OptionPackageCodeArray =[search index]
searchIndex++
end loop
if searchIndex<5
input packageCode
OptionPackageCode[searchIndex] = packageCode
else
Display error "This code does not exist"
end if
4.) Determine totalCost
PackageCostArray[searchIndex] + basePrice = costwithPackage
totalCost = costwithPackage*0.15
5.) display total cost
"The price of your car with Trim is" : totalCost
end loop
and the actual C++ I have written so far is
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declare variables
double basePrice = 0.00;
string OptionPackageCodeArray[] = {"BB", "SP", "NP", "HE", "UC"};
double PackageCostArray [] = {1500.00, 3250.00, 4575.00, 7500.00, 5220.00};
double totalCost = 0.00
//prompt for base price
cout << "Enter base price:";
cin>>basePrice;
cout <<"enter package code: BB, SP, NP, HE, UC";
cin >> OptionPackageCodeArray;
}
however I'm stuck at this point
if anybody has any suggestions I'd be happy to take them.
you just write the code step by step. you can read blow code for reference.
double basePrice = 0.00;
static const int num = 5;
string OptionPackageCodeArray[num] = {"BB", "SP", "NP", "HE", "UC"};
double PackageCostArray [num] = {1500.00, 3250.00, 4575.00, 7500.00, 5220.00};
double totalCost = 0.00;
while(true)
{
//prompt for base price
cout << "Enter base price:";
cin>>basePrice;
std::string package;
cout <<"enter package code: BB, SP, NP, HE, UC"<<std::endl;
cin >> package;
int i = 0;
for (; i < num; i++)
{
if (OptionPackageCodeArray[i] == package)
{
break;
}
}
if (i == num)
{
break;
}
else
{
std::cout<<(basePrice + PackageCostArray[i]) * 0.15<<std::endl;
}
}
I have to write a program for a C++ class and I am having a bit of trouble. I have gone through all my variables and I feel like I initialized everything, but its still not working.
Program parameters: Calculate electrical bill. Customer gets charged $0.27 a kwh up to 500 kwh, then is charged at an extra rate of $0.57 a kwh thereafter.
Input: kwh
output: Total customer bill
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//List the variables
double kwh;
double baseKwh;
double extraKwh;
double baseBill;
double extraBill;
double electricBill;
//User inputs the kwh used
cout << "Enter kwh used: "; //Prompt
cin >> kwh;
//Process the data and compute the bill
if (kwh <= 500) {
baseKwh = kwh;
extraBill = 0;
}
else {
baseKwh = 500;
extraKwh = kwh - 500;
}
baseBill = baseKwh * 0.27;
extraBill = extraKwh * 0.55;
electricBill = baseBill + extraBill;
//Output the bill.
cout << "Your bill is $" << electricBill << endl;
system("PAUSE");
return 0;
}
problem:
Run-Time Check Failure #3 - The variable 'extraKwh' is being used
without being initialized.
Microsoft Visual Studio points to line 30 as the problem. The program works fine when the user inputs above 500, however, when the user inputs 500 or below then I get the error message.
extraBill = extraKwh * 0.55;
Well if the else clause is not evaluated above this code, extraKwh is indeed uninitialized. So you are trying to read value of uninitialized variable above and triggering undefined behaviour. Assign some default value to it during declaration and that issue should go away.
In general it is good idea to initialize variables during declaration.
You have declared your variables, but left them uninitialized.
You are using extraKwh no matter what happens in your code. Judging by your logic, you should initialize it to zero at the same time it is declared:
double extraKwh = 0;
This way, there will always be a value assigned to extraKwh, even if you don't don't hit your else block.
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.
to all programmers enthusiast and professionals out there! I am new at C++ programming. I just want to ask how am I going to approach this task in my program "purchasing item". The user was inputting an item code and this item code has a corresponding value and inputting also the quantity of this item. The program will ask "would you like to purchased another item" if the user said yes the program will loop and need to add all previous value that the user inputted. My problem is how am I going to loop this and total all value of item that the user inputted. We dont use fstream.h
p104 and p103 is an item code
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
main()
{
clrscr();
int p104 = 25, order, total = 0, subtotal, p103 = 22,itemcode, quantity, back;
do{
cout<<"Enter Item Code: ";
cin>>itemcode;
cout<<"\nEnter number of quantity: ";
cin>>quantity;
cout<<subtotal;
total=total+subtotal;
cout<<"\n"<<total;
cout<<"\nWould you like to purchase other item? [Y]-yes?";
cin>>back;
}
while(back=='Y'||back=='y');
getch();
return 0;
}
Use getchar() after cin>>back_; for taking the newline character and calculate subtotal based on condition.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<conio.h>
using namespace std;
main()
{
//clrscr();
char back_;
int p104 = 25, order, total = 0, subtotal =0, p103 = 22,itemcode, quantity;
do{
cout<<"Enter Item Code: ";
cin>>itemcode;
cout<<"\nEnter number of quantity: ";
cin>>quantity;
//cout<<subtotal;
if (itemcode == 104){
subtotal = quantity * p104;
}else if (itemcode == 103){
subtotal = quantity * p103;
}
total=total+subtotal;
subtotal =0;
cout<<"\n"<<total;
cout<<"\nWould you like to purchase other item? [Y]-yes?";
cin>>back_;
getchar();
}
while(back_=='Y'||back_=='y');
return 0;
}
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.