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
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++;
}
Get Answers For Free
Most questions answered within 1 hours.