Question

Language : C ++ #include #include #include #include #include using namespace std; void DisplayMenu() {   ...

Language : C ++

#include
#include
#include
#include
#include
using namespace std;

void DisplayMenu()
{
   cout << "1. E games\n";
   cout << "2. T games\n";
   cout << "3. M games\n";
   cout << "4. Total Games\n";
   cout << "5. Exit\n";


}
double total(double egames, double tgames, double mgames)
{
   int totalgames = egames + tgames + mgames;
   cout << "There are " << totalgames << " games\n";
   return totalgames;
}
double Egames(double egames, double tgames, double mgames)
{
   double Egames;
   Egames = egames/(egames+tgames+mgames);
   cout << "There are " << Egames << " E games\n";
   return Egames;

}
double Tgames(double egames, double tgames, double mgames)
{
   double Tgames;
   Tgames = tgames/(egames + tgames + mgames);
   cout << "There are " << Tgames << " T games\n";
   return Tgames;

}
double Mgames(double egames, double tgames, double mgames)
{
   double Mgames;
   Mgames = mgames/(egames + tgames + mgames);
   cout <<"There are " << Mgames << " M games\n";
   return Mgames;

}
int main()
{
   int choice;
   double egames;
   double tgames;
   double mgames;
   int totalgames;
   ofstream someFile("text_file.txt");
   someFile << "E games\t";
   someFile << "T games\t";
   someFile << "M games\n";
   cout << "Enter number of E games\n";
   cin >> egames;
   someFile << egames;
   someFile << "\t\t";
   cout << "Enter number of T games\n";
   cin >> tgames;
   someFile << tgames;
   someFile << "\t\t";
   cout << "Enter number of M games\n";
   cin >> mgames;
   someFile << mgames;
   someFile << "\t";
   DisplayMenu();
   cin >> choice;
   someFile.close();

   for (; choice >= 1 && choice <= 5;)
   {
       if (choice == 1)
           Egames(egames, tgames, mgames);
       cout << "\n";
       if (choice == 2)
           Tgames(egames, tgames, mgames);
       cout << "\n";
       if (choice == 3)
           Mgames(egames, tgames, mgames);
       cout << "\n";
       if (choice == 4)
           total(egames, tgames, mgames);
       cout << "\n";
       if (choice == 5)
           exit(0);
       DisplayMenu();
       cin >> choice;
   }

}

I need a way to add a 2d array and reading from the file any solutions?

The text file is just called text_file.txt

Homework Answers

Answer #1

Answer:

Just add the header file: #include<bits/stdc++.h>

Just Place the below code inside your main functoin.

In the code, I just created 2d array to store the values that read from file called text_file.txt.

I skip the first line of File - so used check variable.

From the second line of the file, I take the line and split using stringstream with the help of space inbetween the values.

I store the splitted values in a vector.

Then from the vector and I store the values to the array that I have created.

The below code works perfectly and it has no error!!!

Code:

ifstream file;

file.open("text_file.txt");

string line;

int arr[1000][3]={0};

vector<int> temp;

int i=0;
int check=1;
`while(getline(file,line))
{
   temp.clear();
  
   if(check>=2)
   {
  
   stringstream s(line);
  
   while(getline(s,t,' '))
   {
       temp.push_back(stoi(t));
   }
  
   arr[i][0]=v[0];
   arr[i][1]=v[1];
   arr[i][2]=v[2];
  
   i++;
}
check++;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout...
#include <iostream>using namespace std;int main(){ char meal_choice; //user's choice int servings, total_calories; // Display greeting: cout << "Welcome to the Calorie Count-ulator!\n"; // Get user input: cout << "Enter your meal choice ([P]izza, [S]alad, [H]amburger)\n"; cin >> meal_choice; cout << "Enter the amount of servings (1-9):\n"; cin >> servings; // TODO: Use a switch statement to evaluate the user's meal choice // Handle error checking where appropriate // Exit the program: return 0;}
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...