can anyone help me fix this??? I have to do all of this in functions, but i keep getting somethings wrong. Thanks!


Sample Run (The user’s entry is in bold)

Enter the Students Id:5656
Enter how many courses-up to 3: 6
Invalid number! Try again
Enter how many courses-up to 3: 3
Enter the 3 course number(s): 4587 4599 9696
Sorry, you can’t cumulate more than 7 credit hours
Do you want to try again (Y/N): y
Enter how many courses-up to 3: 2
Enter the 2 course number(s): 4587 4599
Thank you!
Press Any Key to Continue...

VALENCIA COMMUNITY COLLEGE
ORLANDO FL 10101
--------------------
Fee Invoice Prepared for Student V5656
1 Credit Hour = $120.25

CRN CR_PREFIX CR_HOURS
4587 MAT 236 4 $ 481.00
4599 COP 220 3 $ 360.75

Health & id fees $ 35.00

--------------------------------------
Total Payments $ 876.75
Goodbye!


code:


#include <iostream>
#include <iomanip>
using namespace std;

void Courses ();
void Receipt ();
void studentid ();

main ()
{
int crn1 = 0;
int crn2 = 0;
int crn3 = 0;
int studentid;
int num;
int course1 = 0;
int course2 = 0 ;
int course3 = 0 ;
double total1;
double total2;
double total3;
double healthfees = 35.00;
double finalprice;
char answer ;


Courses ();

cout <<"Enter the Students Id: ";
cin >> studentid;
cout <<"\n";

cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";

while (num < 1 || num >= 4)
{
cout<<"Invalid number! Try again\n\n";
cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";
}


switch (num)
{
case 3 : cout <<"Enter the 3 course numbers: ";
cin >> course1 >> course2 >> course3;
cout <<"\n";
break;
case 2 : cout <<"Enter the 2 course numbers: ";
cin >> course1 >> course2;
cout <<"\n";
break;
case 1 : cout <<"Enter 1 course number: ";
cin >> course1;
cout <<"\n";
break;
default : cout <<"Invalid number! Try again!\n\n";


system ("pause");
return 0;
}

switch (course1)
{
case 4587 : crn1 = 4;
break;
case 4599 : crn1 = 3;
break;
case 8997 : crn1 = 1;
break;
case 9696 : crn1 = 3;
}

switch (course2)
{
case 4587 : crn2 = 4;
break;
case 4599 : crn2 = 3;
break;
case 8997 : crn2 = 1;
break;
case 9696 : crn2 = 3;
}

switch (course3)
{
case 4587 : crn3 = 4;
break;
case 4599 : crn3 = 3;
break;
case 8997 : crn3 = 1;
break;
case 9696 : crn3 = 3;
}


if (crn1 + crn2 + crn3 > 7);
{
cout<< "Sorry, you can't cumulate more than 7 credit hours.\n\n";
cout<< "Would you like to try again? (Y/N): ";
cin>> answer;
cout<<"\n";

if (answer == 'y');
cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";
}

switch (num)
{
case 3 : cout <<"Enter the 3 course numbers: ";
cin >> course1 >> course2 >> course3;
cout <<"\n";
break;
case 2 : cout <<"Enter the 2 course numbers: ";
cin >> course1 >> course2;
cout <<"\n";
break;
case 1 : cout <<"Enter 1 course number: ";
cin >> course1;
cout <<"\n";
break;
default : cout <<"Invalid number! Try again!\n\n";
}


if (answer == 'n');
{
cout <<"Thank you!\n\n";

}

Receipt ();

//payment calculations
total1 = crn1*120.25;
total2 = crn2*120.25;
total3 = crn3*120.25;
finalprice = healthfees +total1 +total2 +total3;

//Display for receipt

if ( course1 == 4587 || course1==4599 || course1==8997 || course1==9696 )
{


switch (course1)
{
case 4587 : cout<<" "<<course1<<"\t MAT 236\t\t4\t $ "<<total1<<"\n\n";
break;
case 4599 : cout<<" "<<course1<<"\t COP 220\t\t3\t $ "<<total1<<"\n\n";
break;
case 8997 : cout<<" "<<course1<<"\t GOL 124\t\t1\t $ "<<total1<<"\n\n";
break;
case 9696 : cout<<" "<<course1<<"\t COP 100\t\t3\t $ "<<total1<<"\n\n";
break;
}
}
if ( course2 == 4587 || course2==4599 || course2==8997 || course2==9696 )
{

switch (course2)
{
case 4587 : cout<<" "<<course2<<"\t MAT 236\t\t4\t $ "<<total2<<"\n\n";
break;
case 4599 : cout<<" "<<course2<<"\t COP 220\t\t3\t $ "<<total2<<"\n\n";
break;
case 8997 : cout<<" "<<course2<<"\t GOL 124\t\t1\t $ "<<total2<<"\n\n";
break;
case 9696 : cout<<" "<<course2<<"\t COP 100\t\t3\t $ "<<total2<<"\n\n";
break;
}
}
cout<<"\n";
if (course3 == 4587 || course3==4599 || course3==8997 || course3==9696 )
{

switch (course3)
{
case 4587 : cout<<" "<<course3<<"\t MAT 236\t\t4\t $ "<<total3<<"\n\n";
break;
case 4599 : cout<<" "<<course3<<"\t COP 220\t\t3\t $ "<<total3<<"\n\n";
break;
case 8997 : cout<<" "<<course3<<"\t GOL 124\t\t1\t $ "<<total3<<"\n\n";
break;
case 9696 : cout<<" "<<course3<<"\t COP 100\t\t3\t $ "<<total3<<"\n\n";
break;
}
}

cout<<"\n";
cout<<" Health and I.D. fees: $" <<healthfees <<"\n\n";
cout<<" ---------------------------------------------\n";
cout<<" Total Payments: $"<<finalprice <<"\n\n\n";


system ("pause");
}

//********
void Courses ()
{
cout <<"Here is the list of all courses Valencia Community College offers: \n\n";
cout <<"\tCRN\t\tCourse Prefix\t\tCredit Hours\n\n";
cout <<"\t4587\t\t MAT 236\t\t 4\n\n";
cout <<"\t4599\t\t COP 220\t\t 3\n\n";
cout <<"\t8997\t\t GOL 124\t\t 1\n\n";
cout <<"\t9696\t\t COP 100\t\t 3\n\n\n";
}
//********
void Receipt ()
{
cout <<"Here is the student's fee invoice:\n\n\n\n";

cout<<fixed <<setprecision(2);
cout<<" Valencia Community College\n";
cout<<" --------------------------------------------\n\n";
cout<<" Fee invoice prepared for student V"<<studentid<<"\n\n";
cout<<" 1 credit hour= $120.25\n\n\n";
cout <<" CRN Course Prefix Credit Hours\n\n\n";
}

Dani AI

Generated

Three quick root causes explain the behavior reported by and flagged by : stray semicolons after if statements, a scope/name mismatch for studentid (a function prototype and a variable with the same name), and input/credit-mapping done only once so a “try again” path leaves old credit values in place. Fixing those three items and structuring input+validation into a single loop will make the program predictable.

A robust flow (pseudocode) is:

loop {
  get student id
  get number of courses (validate 1..3)
  read that many CRNs
  map each CRN to credit hours
  if sum of credits <= 7: break
  ask try-again (Y/N); if N: exit
}
compute totals and call receipt-printing function with the collected data

Recommended modular functions (signatures only) to avoid scope and reuse problems:

int getStudentId();
int getNumCourses();
std::vector<int> readCourses(int num);
int creditForCRN(int crn);            // returns 0 if CRN invalid
void printReceipt(int studentId, const std::vector<int>& crns);

Implementation notes and cautions:

  • Remove stray semicolons after if(...)if (cond); { ... } makes the block run unconditionally. pointed this out; it is the cause for the “keeps going” symptom.
  • Do not declare a function and a variable with the same identifier; either remove void studentid() or rename it (or make studentId a global deliberately, but passing it as a parameter is cleaner).
  • Recompute credit hours after every re-entry. Reset per-course credit counters before remapping.
  • Accept both Y and y (use toupper()), and prefer return 0; from main() over incorrectly written exit 0; (if exit is used, write exit(0); and include <cstdlib>).
  • Validate CRNs (report and re-prompt on invalid entry) and format money output with fixed and setprecision(2).

Following the loop + mapping approach and passing data into printReceipt keeps scope correct and eliminates stale values that produced an extra course line in the invoice.

Recommended Answers

All 11 Replies

Explain your problem

when the program ask to try again yes or no, no matter what you put it keeps going, instead of stopping when u put no; when u put 2 courses, it displays 3 in the receipt, and i would like to put the receipt as a function, but don't know how to do it.

CORRECT IT

if (answer == 'y');
{cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n"}

.

>> You cant declare function and integer name same in a program . Then how did you run the program.

You can use

else 
                             exit(0);

for finishing the program or goto to go back .

That semicolon with if statement is a mistake remove that please when You correct the program.

i'm using dev c++ to run the program... if i put else, it displays " expected primary-expression before "else" and expected `;' before "else", but no ; is missing...

After else always there is a statement and using ; tells that statement is complete specially with if dont use ; it means that if statement has no outer coverge to other statement which make it of no use.

Why dont you try this one

if (answer ==  'y')
{
cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";
}

similarly correct your other if statement.

if (crn1 + crn2 + crn3 > 7);  if (answer == 'n');

These are mistakes.

#include <iostream>
#include <iomanip>
#include<stdlib>
using namespace std;

void Courses ();
void Receipt ();
void studentid ();



main ()
{
int crn1 = 0;
int crn2 = 0;
int crn3 = 0;
int studentid;
int num;
int course1 = 0;
int course2 = 0 ;
int course3 = 0 ;
double total1;
double total2;
double total3;
double healthfees = 35.00;
double finalprice;
char answer ;


Courses ();

cout <<"Enter the Students Id: ";
cin >> studentid;
cout <<"\n";

cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";

while (num < 1 || num >= 4)
{
cout<<"Invalid number! Try again\n\n";
cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";
}


switch (num)
{
case 3 : cout <<"Enter the 3 course numbers: ";
cin >> course1 >> course2 >> course3;
cout <<"\n";
break;
case 2 : cout <<"Enter the 2 course numbers: ";
cin >> course1 >> course2;
cout <<"\n";
break;
case 1 : cout <<"Enter 1 course number: ";
cin >> course1;
cout <<"\n";
break;
default : cout <<"Invalid number! Try again!\n\n";


system ("pause");
return 0;
}

switch (course1)
{
case 4587 : crn1 = 4;
break;
case 4599 : crn1 = 3;
break;
case 8997 : crn1 = 1;
break;
case 9696 : crn1 = 3;
}

switch (course2)
{
case 4587 : crn2 = 4;
break;
case 4599 : crn2 = 3;
break;
case 8997 : crn2 = 1;
break;
case 9696 : crn2 = 3;
}

switch (course3)
{
case 4587 : crn3 = 4;
break;
case 4599 : crn3 = 3;
break;
case 8997 : crn3 = 1;
break;
case 9696 : crn3 = 3;
}


if (crn1 + crn2 + crn3 > 7)
{
cout<< "Sorry, you can't cumulate more than 7 credit hours.\n\n";
cout<< "Would you like to try again? (Y/N): ";
cin>> answer;
cout<<"\n";
if (answer == 'y')
{
cout <<"Enter how many courses-up to 3: ";
cin >> num;
cout <<"\n";
switch (num)
{
case 3 : cout <<"Enter the 3 course numbers: ";
cin >> course1 >> course2 >> course3;
cout <<"\n";
break;
case 2 : cout <<"Enter the 2 course numbers: ";
cin >> course1 >> course2;
cout <<"\n";
break;
case 1 : cout <<"Enter 1 course number: ";
cin >> course1;
cout <<"\n";
break;
default : cout <<"Invalid number! Try again!\n\n";
}
else if (answer == 'n');
{
cout <<"Thank you!\n\n";
exit 0;
}
}
Receipt ();



//payment calculations
total1 = crn1*120.25;
total2 = crn2*120.25;
total3 = crn3*120.25;
finalprice = healthfees +total1 +total2 +total3;

//Display for receipt

if ( course1 == 4587 || course1==4599 || course1==8997 || course1==9696 )
{


switch (course1)
{
case 4587 : cout<<" "<<course1<<"\t MAT 236\t\t4\t $ "<<total1<<"\n\n";
break;
case 4599 : cout<<" "<<course1<<"\t COP 220\t\t3\t $ "<<total1<<"\n\n";
break;
case 8997 : cout<<" "<<course1<<"\t GOL 124\t\t1\t $ "<<total1<<"\n\n";
break;
case 9696 : cout<<" "<<course1<<"\t COP 100\t\t3\t $ "<<total1<<"\n\n";
break;
}
}
if ( course2 == 4587 || course2==4599 || course2==8997 || course2==9696 )
{

switch (course2)
{
case 4587 : cout<<" "<<course2<<"\t MAT 236\t\t4\t $ "<<total2<<"\n\n";
break;
case 4599 : cout<<" "<<course2<<"\t COP 220\t\t3\t $ "<<total2<<"\n\n";
break;
case 8997 : cout<<" "<<course2<<"\t GOL 124\t\t1\t $ "<<total2<<"\n\n";
break;
case 9696 : cout<<" "<<course2<<"\t COP 100\t\t3\t $ "<<total2<<"\n\n";
break;
}
}
cout<<"\n";
if (course3 == 4587 || course3==4599 || course3==8997 || course3==9696 )
{

switch (course3)
{
case 4587 : cout<<" "<<course3<<"\t MAT 236\t\t4\t $ "<<total3<<"\n\n";
break;
case 4599 : cout<<" "<<course3<<"\t COP 220\t\t3\t $ "<<total3<<"\n\n";
break;
case 8997 : cout<<" "<<course3<<"\t GOL 124\t\t1\t $ "<<total3<<"\n\n";
break;
case 9696 : cout<<" "<<course3<<"\t COP 100\t\t3\t $ "<<total3<<"\n\n";
break;
}
}

cout<<"\n";
cout<<" Health and I.D. fees: $" <<healthfees <<"\n\n";
cout<<" ---------------------------------------------\n";
cout<<" Total Payments: $"<<finalprice <<"\n\n\n";




system ("pause");
}

//********
void Courses ()
{
cout <<"Here is the list of all courses Valencia Community College offers: \n\n";
cout <<"\tCRN\t\tCourse Prefix\t\tCredit Hours\n\n";
cout <<"\t4587\t\t MAT 236\t\t 4\n\n";
cout <<"\t4599\t\t COP 220\t\t 3\n\n";
cout <<"\t8997\t\t GOL 124\t\t 1\n\n";
cout <<"\t9696\t\t COP 100\t\t 3\n\n\n";
}
//********
void Receipt ()
{
cout <<"Here is the student's fee invoice:\n\n\n\n";

cout<<fixed <<setprecision(2);
cout<<" Valencia Community College\n";
cout<<" --------------------------------------------\n\n";
cout<<" Fee invoice prepared for student V"<<studentid<<"\n\n";
cout<<" 1 credit hour= $120.25\n\n\n";
cout <<" CRN Course Prefix Credit Hours\n\n\n";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.