新的C ,模块和开关菜单

New to C++, modules and switch menu

本文关键字:开关 菜单 模块 新的      更新时间:2023-10-16

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

我认为先制作单个程序,然后将它们与开关案例拼凑在一起,并且功能很容易(我不知道班级/对象(。我一直遇到砖墙和许多语法/逻辑错误,所以我删除了所有内容,现在我又回到了正方形的一个 - 带有三个不同的程序。这是非常艰巨的 - 如何进行?:/


#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 subjectsn";
  //  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 << ".nn";
      outfile << "The average score for student " << student;
      outfile << " is " << average << ".nn";
    }
    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 openn";
    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. 创建一个主文件作为应用程序的入口点,请注意函数的原型和实现 printa printb

#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. 对另一个文件中的打印b做同样的事情。

  3. 从main.cpp

  4. 中删除这些实现

你完成了!