I need help with a c++ project for school.

Strokes

New Member
hey guys, I have to to make a store in c++ for school and have several problems I hope you can help with. Heres my code:




#include <iostream.h>
#include <windows.h> // allows color to be used

void check_out();


int main()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //makes color, 10= bright green
float ramcost = 12.00; //this will be the cost of the items
float ram2cost = 20.00;
float hddcost = 55.00;
float hddicost = 100.00;
float fancost = 12.00;
float cpuheatsinkcost = 40.00;
float cddrivecost = 18.00;
float blurayburnercost = 78.00;
float newtotal1 = 0;
float newtotal2 = 0;
float newtotal3 = 0;
float newtotal4 = 0;
float newtotal5 = 0 ;
float newtotal6 = 0;
float newtotal7 = 0;
float newtotal8 = 0;
float moneytotal = 1;

int choice;
int ram = 1;
int ram2 = 1;
int hdd = 1;
int hddi = 1;
int fan = 1;
int cpuheatsink = 1;
int cddrive = 1;
int blurayburner = 1;
int numbertotal = 1;

int totalram = 0;
int totalram2 = 0;
int totalhdd = 0;
int totalhddi = 0;
int totalfan = 0;
int totalcpuheatsink = 0;
int totalcddrive = 0;
int totalblurayburner = 0;





cout << " Welcome to the store" << endl;
cout << " *If you would like to restart, please press '0'*" << endl;
cout << " To checkout out press '9'" << endl;
do
{
cout << " \n";
cout << " Use numbers 1-8 to choose the product\n"; //instructions
cout << " you would like to buy:\n";
cout << " 1 - 1 GB stick of RAM costs $" << ramcost << '\n';
cout << " 2 - 2 GB stick of RAM costs $" << ram2cost << '\n';
cout << " 3 - 250 GB Internal Hard drive costs $" << hddcost << '\n';
cout << " 4 - 750 GB External USB hard drive costs $" << hddicost << '\n';
cout << " 5 - 120MM 3000RPM case fan costs $" << fancost << '\n';
cout << " 6 - Universal processor heatsink and fan costs $" << cpuheatsinkcost << '\n';
cout << " 7 - CD/DVD drive (no burner) costs $" << cddrivecost <<'\n';
cout << " 8 - Blu~ray/CD/DVD drive (and burner) costs $" << blurayburnercost << '\n';
cout << " 9- checkout" << '\n';
cout << " 0 - RESET" << '\n';


cin >> choice;

if (choice == 9)
{
check_out();
break;
}

switch (choice)
{

case 0:
moneytotal = moneytotal - moneytotal;
numbertotal = numbertotal - numbertotal;
break;

case 1:
newtotal1 = newtotal1 + ramcost;
totalram = totalram + ram;
break;

case 2:
newtotal2 = newtotal2 + ram2cost;
totalram2 = totalram2 + ram2;
break;

case 3:
newtotal3 = newtotal3 + hddcost;
totalhdd = totalhdd + hdd;
break;

case 4:
newtotal4 = newtotal4 + hddicost;
totalhddi = totalhddi + hddi;
break;

case 5:
newtotal5 = newtotal5 + fancost;
totalfan = totalfan + fan;
break;

case 6:
newtotal6 = newtotal6 + cpuheatsinkcost;
totalcpuheatsink = totalcpuheatsink + cpuheatsink;
break;

case 7:
newtotal7 = newtotal7 + cddrivecost;
totalcddrive = totalcddrive + cddrive;
break;

case 8:
newtotal8 = newtotal8 + blurayburnercost;
totalblurayburner = totalblurayburner + blurayburner;
break;

}

cout << " " << '\n';
cout << " Your total is: $" << (moneytotal = newtotal1 + newtotal2 + newtotal3 + newtotal4 + newtotal5 + newtotal6 + newtotal7 + newtotal8) << '\n';
cout << " Number if items in cart:"<< (numbertotal = totalram + totalram2 + totalhdd + totalhddi + totalfan + totalcpuheatsink + totalcddrive + totalblurayburner) << '\n';



}
while (1);
{
cout << " Good buy." << '\n';
}


return 0;
}

void check_out()
{
int totalram;
int totalram2;
int totalhdd;
int totalhddi;
int totalfan;
int totalcpuheatsink;
int totalcddrive;
int totalblurayburner;


float newtotal1;
float newtotal2;
float newtotal3;
float newtotal4;
float newtotal5;
float newtotal6;
float newtotal7;
float newtotal8;
float moneytotal;


cout << "Here is your receipt:" << '\n';
cout << "Item number and name: Number bought: Total cost:" << '\n';// the values below may appear out of order, but they are in the window.
cout << "1 - 1 GB stick of RAM " << totalram << newtotal1 << '\n';
cout << "2 - 2 GB stick of RAM " << totalram2 << newtotal2 << '\n';
cout << "3 - 250 GB Internal SATA Hard drive " << totalhdd << newtotal3 << '\n';
cout << "4 - 750 GB External USB hard drive " << totalhddi << newtotal4 << '\n';
cout << "5 - 120MM 3000RPM case fan " << totalfan << newtotal5 << '\n';
cout << "6 - Universal processor heatsink and fan" << totalcpuheatsink << newtotal6 << '\n';
cout << "7 - CD/DVD drive (no burner) " << totalcddrive << newtotal7 << '\n';
cout << "8 - Blu~ray/CD/DVD drive (and burner) " << totalblurayburner << newtotal8 << '\n';



}


so here are my problems:
If any letter is pushed it loops infinitely.
I cant get the checkout function to show the amount items or $
The reset (case 0:) wont work
So any help for a n00b would be appreciated.
 
hey guys, I have to to make a store in c++ for school and have several problems I hope you can help with. Heres my code:




#include <iostream.h>
#include <windows.h> // allows color to be used

void check_out();

float newtotal1 = 0;
float newtotal2 = 0;
float newtotal3 = 0;
float newtotal4 = 0;
float newtotal5 = 0 ;
float newtotal6 = 0;
float newtotal7 = 0;
float newtotal8 = 0;
float moneytotal = 1;
int totalram = 0;
int totalram2 = 0;
int totalhdd = 0;
int totalhddi = 0;
int totalfan = 0;
int totalcpuheatsink = 0;
int totalcddrive = 0;
int totalblurayburner = 0;


int main()
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //makes color, 10= bright green
float ramcost = 12.00; //this will be the cost of the items
float ram2cost = 20.00;
float hddcost = 55.00;
float hddicost = 100.00;
float fancost = 12.00;
float cpuheatsinkcost = 40.00;
float cddrivecost = 18.00;
float blurayburnercost = 78.00;


int choice;
int ram = 1;
int ram2 = 1;
int hdd = 1;
int hddi = 1;
int fan = 1;
int cpuheatsink = 1;
int cddrive = 1;
int blurayburner = 1;
int numbertotal = 1;





cout << " Welcome to the store" << endl;
cout << " *If you would like to restart, please press '0'*" << endl;
cout << " To checkout out press '9'" << endl;
do
{
cout << " \n";
cout << " Use numbers 1-8 to choose the product\n"; //instructions
cout << " you would like to buy:\n";
cout << " 1 - 1 GB stick of RAM costs $" << ramcost << '\n';
cout << " 2 - 2 GB stick of RAM costs $" << ram2cost << '\n';
cout << " 3 - 250 GB Internal Hard drive costs $" << hddcost << '\n';
cout << " 4 - 750 GB External USB hard drive costs $" << hddicost << '\n';
cout << " 5 - 120MM 3000RPM case fan costs $" << fancost << '\n';
cout << " 6 - Universal processor heatsink and fan costs $" << cpuheatsinkcost << '\n';
cout << " 7 - CD/DVD drive (no burner) costs $" << cddrivecost <<'\n';
cout << " 8 - Blu~ray/CD/DVD drive (and burner) costs $" << blurayburnercost << '\n';
cout << " 9- checkout" << '\n';
cout << " 0 - RESET" << '\n';


cin >> choice;

if (choice == 9)
{
check_out();
break;
}

switch (choice)
{

case 0:
moneytotal = moneytotal - moneytotal;
numbertotal = numbertotal - numbertotal;
break;

case 1:
newtotal1 = newtotal1 + ramcost;
totalram = totalram + ram;
break;

case 2:
newtotal2 = newtotal2 + ram2cost;
totalram2 = totalram2 + ram2;
break;

case 3:
newtotal3 = newtotal3 + hddcost;
totalhdd = totalhdd + hdd;
break;

case 4:
newtotal4 = newtotal4 + hddicost;
totalhddi = totalhddi + hddi;
break;

case 5:
newtotal5 = newtotal5 + fancost;
totalfan = totalfan + fan;
break;

case 6:
newtotal6 = newtotal6 + cpuheatsinkcost;
totalcpuheatsink = totalcpuheatsink + cpuheatsink;
break;

case 7:
newtotal7 = newtotal7 + cddrivecost;
totalcddrive = totalcddrive + cddrive;
break;

case 8:
newtotal8 = newtotal8 + blurayburnercost;
totalblurayburner = totalblurayburner + blurayburner;
break;

}

cout << " " << '\n';
cout << " Your total is: $" << (moneytotal = newtotal1 + newtotal2 + newtotal3 + newtotal4 + newtotal5 + newtotal6 + newtotal7 + newtotal8) << '\n';
cout << " Number if items in cart:"<< (numbertotal = totalram + totalram2 + totalhdd + totalhddi + totalfan + totalcpuheatsink + totalcddrive + totalblurayburner) << '\n';



}
while (1);
{
cout << " Good buy." << '\n';
}


return 0;
}

void check_out()
{


float newtotal1;
float newtotal2;
float newtotal3;
float newtotal4;
float newtotal5;
float newtotal6;
float newtotal7;
float newtotal8;
float moneytotal;


cout << "Here is your receipt:" << '\n';
cout << "Item number and name: Number bought: Total cost:" << '\n';// the values below may appear out of order, but they are in the window.
cout << "1 - 1 GB stick of RAM " << totalram << newtotal1 << '\n';
cout << "2 - 2 GB stick of RAM " << totalram2 << newtotal2 << '\n';
cout << "3 - 250 GB Internal SATA Hard drive " << totalhdd << newtotal3 << '\n';
cout << "4 - 750 GB External USB hard drive " << totalhddi << newtotal4 << '\n';
cout << "5 - 120MM 3000RPM case fan " << totalfan << newtotal5 << '\n';
cout << "6 - Universal processor heatsink and fan" << totalcpuheatsink << newtotal6 << '\n';
cout << "7 - CD/DVD drive (no burner) " << totalcddrive << newtotal7 << '\n';
cout << "8 - Blu~ray/CD/DVD drive (and burner) " << totalblurayburner << newtotal8 << '\n';



}


so here are my problems:
If any letter is pushed it loops infinitely.
I cant get the checkout function to show the amount items or $
The reset (case 0:) wont work
So any help for a n00b would be appreciated.

Well Im a noob at c but it seems like your variables need to be initiated before the int main(). look back for red

again my knowledge of c is from microcontroller programing
 
Instead of multiple newtotalx variables, you should be using an array.

Anyways, the problem here seems to be here:
Code:
cin >> choice;
This statement puts the ASCII value of the corresponding letter/number into the variable "choice". Here, you can see that numbers 0-9 are represented by 48-57, so you should be comparing these values instead.

Code:
while (1);
{
cout << " Good buy." << '\n';
}
And this is an infinite loop. The condition (1) always equals true, and you have no break/goto or anything else to manually get out of the loop, so once it gets there, it stays there until you reset the progra.

EDIT: Also, ALWAYS wrap your code in CODE tags ([ CODE] and [ /CODE] without the spaces), otherwise all formatting is lost and it's a huge PITA to read.
 
Back
Top