C ++,模块和开关菜单的新手

时间:2017-05-04 05:37:01

标签: c++ c++11

我正在开发一个项目,该项目涉及创建菜单屏幕,并将三个程序集成到一个可以从菜单屏幕中选择性启动的程序。

我认为首先制作单独的程序,然后将它们与开关盒拼接在一起并且功能很简单(我不知道类/对象)。我一直遇到一堵砖墙和一系列语法/逻辑错误,所以我删除了所有内容,现在我又回到原点 - 有三个不同的程序。这非常令人生畏 - 怎么办? :/

#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;
}

1 个答案:

答案 0 :(得分:1)

我认为你是在徒步射击自己。一步接一步,请把它作为参考:

  1. 创建一个主文件作为应用的入口点,注意功能的原型和实现 printA printB
  2. #include <iostream>
    using namespace std;
    
    void printA();
    void printB();
    
    int main()
    {
        int x = 1;
        cout << "Enter the option:\n";
        while (cin >> x)
        {
            if (x <= 0)
            {
                break;
            }
            switch (x) {
            case 1:
                printA();
                break;
            case 2:
                printB();
                break;
            default:
                cout << "Invalid";
            }
        }
        return 0;
    }
    
    
    void printB()
    {
        cout << "B";
    }
    
    void printA()
    {
        cout << "A";
    }
    
    1. 创建一个新文件fooA.cpp并粘贴已实现的函数printA

    2. 对另一个文件中的printB执行相同的操作。

    3. 从main.cpp中删除这些实现

    4. 你完成了!