mercredi 3 mai 2017

New to C++, modules and switch menu

Working on a project that involves creating a menu screen, and integrating three programs into one that can be selectively launched from the menu screen.

I thought making individual programs first before piecing them together with switch case and functions would be easy (I don't know class/objects). I keep running into a brick wall and a host of snytax/logical errors, so I deleted everything and now i'm back to square one-- with three distinct programs. This is very daunting-- how do i proceed? :/

#include <iostream>

using namespace std;

int main ()
{
int x;
cout<<"Enter the amount of money you have ";
cin>>x;
if (x >= 20)
 {
cout<<"You can buy Comic Book $1 or Sports Journal $3 or Science Book $15 or 
Third Volume Series $20"; 
}
else if (x >=15 && x < 20)
{
cout<<"You can buy Comic Book $1 or Sports Journal $3 or Science book $15";
}
else if (x >=3 && x < 15)
{
cout<<"You can buy Comic Book $1 or Sports Journal $3";
}
else if (x >=1 && x < 3)
{
cout<<"You can buy Comic Book $1";
}
else if (x <=0)
{
 cout<<"You cannot afford anything";
}
return 0;
}


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

int main()
 {
int numclass; //number of classes
 int numStudents; //number of students
int numTests; // Number of tests per student
double sectiontotal; //Accumulator for Class total scores
 double total; // Accumulator for total scores
double average; //Average test score
double totalaverage=0;
ofstream outfile;
outfile.open("Gradesinfo.txt");
// Set up numeric output formatting.
 cout << fixed << showpoint << setprecision(1);

// Get the number of students.
 cout << "This program gives average of test scores per student, per class, and for the whole institution for upto 2 students in upto 2 different classes in 3 subjects\n";

//  Determine each student's average score.
cout<<"Enter the number of classes";
cin>>numclass;
cout <<"Enter the number of students per class";
cin>>numStudents;
cout<<"Enter the number of tests";
cin>>numTests;
   for (int section = 1; section <= numclass; section++)
 {
     sectiontotal = 0; //Initialize class accumulator
     totalaverage=0;
 for (int student = 1; student <= numStudents; student++)
 {
    total = 0; // Initialize the accumulator.
        for (int test = 1; test <= numTests; test++)
    {
    double score;
    cout << "Enter score " << test << " for ";
    cout << "student " << student << ": ";
    cin >> score;
    if(score>=0 && score<=100){
    total += score;
    }
    else{
    cout<<"Enter value from 1 - 100" <<endl;
    test=test-1;
    }
    }
 average = total / numTests;
 totalaverage+=average;
 cout << "The average score for student " << student;
 cout << " is " << average << ".\n\n";
 outfile << "The average score for student " << student;
 outfile << " is " << average << ".\n\n";
 }
 cout << "The total average for class " << section <<" is: "<< totalaverage/numStudents <<endl;
 outfile << "The total average for class " << section << " is: " << totalaverage/numStudents <<endl;
 sectiontotal+= totalaverage;
  }
 cout <<"The total average for the institution is: "<< sectiontotal/numclass;
 outfile <<"The total average for the institution is: "<< sectiontotal/numclass;
 outfile.close();
  return 0;
 }

#include<cstdlib>
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    fstream instream;
    ofstream outstream;
    instream.open("Grades.txt");
    if(instream.fail())
    {
        cout<<"The input file failed to open\n";
        exit(1);
    }

    int next, largest, smallest;
    largest = 0;
    smallest = 0;


    while(instream>>next)
    {

        if(largest<next)
        {
            largest = next;
        }
        else{
                  smallest = next;
        }
    }

    cout<<"The highest grade is: "<<largest<<endl;
    instream.close();
    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire