http://pastebin.com/eC09W08p This project will focus on file processing. The ide
ID: 3537493 • Letter: H
Question
Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int tries=0;
char *filename;
ifstream input;
do{
if(tries>0)
{
cout<<"Error opening file. Please try again."<<endl;
if(tries%3 == 0) { cout<<"Please make sure that the file you are trying to open exists."<<endl; }
}
cout<<"Please give the filename:";
cin>>filename;
tries++;
input.open(filename);
}while(!input.is_open());
int choice;
int read=0;
int deg;
input>>deg;
ofstream output;
int values[deg][2];
while(true)
{
cout<<"COP2271 Data Manager MAIN MENU 1. To read graph from data file. 2. To compute the number of outgoing arcs from a certain node. 3. To compute the number of incoming arcs to a certain node. 0. To quit. Please choose:";
cin>>choice;
switch(choice)
{
case 1:
{
if(read==1) { cout<<"The file has already been read once"<<endl; break; }
for(int i=0;i<deg;i++)
{ input>>values[i][0]>>values[i][1];
}
cout<<"Graph successfully inputted! The graph has "<<deg<<" arcs."<<endl;
input.close();
read=1;
break;
}
case 2:
case 3:
{
if(read==0) { cout<<"The file has not been read yet."<<endl; break; }
int node;
cout<<"Please give the node you are looking for:";
cin>>node;
int nodeCount=0;
int x;
string x2;
if( choice ==2 ) { x=0; x2="leaving"; }
else if( choice ==3 ) { x=1; x2="going to"; }
for(int i=0;i<deg;i++)
{ if(values[i][x] == node) nodeCount++; }
cout<<"There are "<<nodeCount<<" arcs "<<x2<<" node "<<node<<endl;
output.open("output.txt",ios::out | ios::app);
output<<"There are "<<nodeCount<<" arcs "<<x2<<" node "<<node<<endl;
output.close();
cout<<"Report stored in file: output.txt."<<endl;
break;
}
case 0:
cout<<"Now quitting..."<<endl;
return 0;
break;
default:
cout<<"Wrong choice. Please choose again!"<<endl;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.