GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-May-2008, 22:41
CBNewbie CBNewbie is offline
New Member
 
Join Date: May 2008
Posts: 2
CBNewbie is on a distinguished road

C++ help...


I really need help with a problem for a class. I'm completely lost on what to do. And, it's due tomorrow. Anyone that can help, please do, anything is greatly appreciated...

My problem is this:

I have a text file with the following in it

Code:
Abilene Amarillo 200 Abilene Dallas 220 Abilene Waco 360 Amarillo Dallas 350 Amarillo Waco 440 Dallas Waco 95

I have to read that in (no problem doing that), then my output must look like this:

Code:
Abilene Amarillo Dallas Waco Abilene 0 200 220 360 Amarillo 200 0 350 440 Dallas 220 350 0 95 Waco 360 440 95 0

The requirements are:

1 dimensional array for city names
2 dimensional array for the distance
function to read the text
function to display the output

Here's what I've currently got down, not much, since I keep re-writing it trying to figure it out...

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <iostream>
#include <iomanip>
 
using namespace std;
 
void readFile1(ifstream &dataFile, string &city1Grab,  string &city2Grab, int &distanceGrab, int &i);
void display1(string city1[], int distance[], int i, int j);
string center(int width,string str1);
 
const int numCities=6;
 
int main()
{
    string header1="Texas Department of Transportation";
    string header2="Mileage chart";
    string city1Grab, city2Grab, city1[numCities]={"x"}, city2[numCities]={"x"};
    int distanceGrab, distance[6]={0}, i=0, j=0, x=0;
    string city[4]={"no"}, test;
 
    for(i=0;i<4;i++)
    {
        city1[i]="x";
        cout << city1[i] << endl;
    }
 
     cout << center(80,header1);
     cout << center(80,header2) << endl << endl;
 
    ifstream dataFile;
    dataFile.open ("dot.txt");
    readFile1(dataFile, city1Grab,  city2Grab, distanceGrab, i);
 
 
        i=0;
        //j=1;
        x=0;
    while(dataFile)
    {
        city1[i]=city1Grab;
        city2[i]=city2Grab;
        distance[x]=distanceGrab;
        //cout << city1[i] << "   " << city1[j] << "   " << distance[x] << endl;
        i++;
        x++;
        readFile1(dataFile, city1Grab,  city2Grab, distanceGrab, i);
 
    }
 
 
 
for(i=0;i<numCities;i++)
{
    cout << "city " << i << " = " << city1[i] << "   "
         << "city2 " << i << " = " << city2[i] << "   "
         << "distance" << i << " = " << distance[i] << endl;
}/*
for(i=0;i<6;i++)
{
    cout << "distance" << i << " = " << distance[i] << endl;
}*/
 
display1(city1, distance, i, j);
    return 0;
    dataFile.close();
}
 
void readFile1(ifstream &dataFile, string &city1Grab, string &city2Grab, int &distanceGrab, int &i)
{
    dataFile >> city1Grab >> city2Grab >> distanceGrab;
 
    return;
}
 
void display1(string city1[], int distance[], int i, int j)
{
    cout << setw(10) << left << " ";
    for(i=0;i<numCities;i+=2)
    {
        cout << setw(10) << left << city1[i];
    }
 
    return;
}
 
string center(int width,string str1)
{
    int sizeOfString;
    int padding;
    sizeOfString=str1.length();
    if ( width > sizeOfString)
    {
        padding=static_cast<float>((width-sizeOfString)/2);
        for (int i=0;i<padding;i++)
        {
        str1=' '+str1;
        }
        for (int i=0;i<padding;i++)
        {
        str1=str1+' ';
        }
    }
 
return str1;
}

I hate to ask for help on a class assignment, I'm just desperate, since it's due tomorrow. I'll be up all night, working on trying to figure this one out.
I assume I'm missing something really obvious, since I've had no problem doing everything else in the class. Any help, at all, would be greatly appreciated. I just need to get this finished so I don't get a zero, and I can understand what I'm missing...

Thanks...

I hate to ask for help, as I usually figure these things out myself..
Last edited by admin II : 05-May-2008 at 04:21. Reason: Changed [CODE] to [CPP]
  #2  
Old 06-May-2008, 04:47
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 434
Peter_APIIT is an unknown quantity at this point

Re: C++ help...


You just to format when you print to file.

I hope this help.
  #3  
Old 06-May-2008, 08:36
J-M J-M is offline
New Member
 
Join Date: Apr 2008
Posts: 14
J-M is on a distinguished road

Re: C++ help...


I recommend reading some tutorial about C++.
http://www.arachnoid.com/cpptutor/index.html
Solution to your problem is in this page:
http://www.arachnoid.com/cpptutor/student3.html
  #4  
Old 06-May-2008, 13:42
zatora zatora is offline
New Member
 
Join Date: May 2008
Posts: 25
zatora is on a distinguished road

Re: C++ help...


all i did is read from ur file by the way u need to leave one space after each data, i am not quite done yet but may be this will help u a little
CPP / C++ / C Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	int i,j,a,b,temp;
	int num[4][4];
	ifstream infile;
	ofstream outfile;
	string str1[4];
	string str2[4];
	infile.open("data.txt");
	outfile.open("output.txt");
	
	while(!infile.eof())
	{
		for(i=0;i<4;i++)
		{
			for(j=1+i;j<4;j++)
			{
				infile>>str1[i]>>str2[j]>>num[i][j];
				temp=num[i][j];
				a=j;b=i;num[a][b]=temp;
				
				
			}
			
		}
		for(i=0;i<4;i++)
		{	
			j=i;
			num[i][j]=0;
		}
		for(i=0;i<4;i++)
			for(j=0;j<4;j++)
				cout<<i<<" "<<j<<" "<<num[i][j]<<endl;

		
	
	}
	
		
	infile.close();
		outfile.close();
		return 0;
}
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:17.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.