2017-05-04 44 views
-1

我正在开发一个涉及创建菜单屏幕的项目,并将三个程序集成到一个可从菜单屏幕中选择启动的程序。我认为先制作个别程序,然后再将它们与开关盒和功能拼接在一起很容易(我不知道类/对象)。我不断遇到一堵砖墙和一系列语法/逻辑错误,所以我删除了所有内容,现在我又回到了原点 - 有三个不同的程序。这非常令人生畏 - 如何继续? :/C++,模块和开关菜单新手


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

第一件事情是:您的应用程序不能有超过1个入口点或主要方法,因此选择一个文件作为主入口并清除其余部分, –

+0

请修复缩进。而'else'部分中的'x <15','x <3' ...是多余的,因为它们在那个时候总是成立的。 –

回答

1

我认为你的脚搬起石头砸自己。走1步前一后,请不要把这个作为参考:

  1. 创建一个主文件入口点的应用程序,需要注意的功能原型和实施printAprintB

#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"; 
} 
  • 创建一个新文件fooA.cpp并粘贴有第i mplemented函数printA

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

  • 从main.cpp中

  • 完成后删除这些实现!

    +1

    修正了它!非常感谢。 –

    +0

    欢迎您! –