• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED C++ DUDES ...lol

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Xtreme Barton

Member
Joined
Jan 17, 2004
back again .. cramming it in as fast as i can .. only got a couple weeks left !!

this little project i got going now consist of reading files of certain data type and storing them in an array as they appear in the file.

Example:

Jan
Feb
Mar
Apr
etc..

so i read that file into a string array .. and i can output just fine that way too. Here is the kicker. Im trying to get output as follows.

Jan Feb Mar Apr etc.....

Im still searching away but if anyone has some pointers :grouphug:

must be able to manipulate integers also in the same fashion. Basically he wants us to reverse output direction of the stored array
 
So you're talking about a 2-Dimensional array? Your code to write the file will depend on the format it needs to be.

For the record, the most common format by far that I work with for 2D data grids is CSV. In the real world I never have to write the code to actually write the files, there are libraries for that :)
 
actually i need to stick with one dimmensional arrays .. each file will get its own array and store each set of integers or strings. i then need to output that in various ways to represent a weather chart for my local area.
 
Your on the right track,

What compiler are you using because converting read in string to int can be done a few different ways.
 
We are required to use visual studio 2010. And unfortunately i may not change its data type in anyway.


I think i have it down a little better. On my way to school for my VB class so ill post what i have when i get in.
 
string to int ?? :bang head

could read like i did string file .. store in array ..convert to int ..:rofl:
 
yep

theres a few ways to do it.

int catch = atoi(string.cStr()); is one of the most common ways to do it.
 
oh man .. tired ..eye's strained ..but i got it done just in the nick of time :attn:

here is my final output .. went about it a little different .. could have done better with some array work but i was in a hurry for final output .

Code:
/** Malik, D.S., (2010). C++ Programming Program Design 
 Including Data Structures, 5th Edition. Boston: Course Technology.
 Modifications by Wm Bowers, 2007 – 2012.
 Additional modifications by D. Keller, 2012.
 Input/Output with files 2000-2012.
 Retrieved from [URL]http://www.cplusplus.com/doc/tutorial/files/[/URL]
 (2012, May 16).
*/
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>
 
using namespace std;
 
void getMonths();
void getHighs();
void getLowTemps();
void getPrecipitation();
void getRecordHighYear();
void getRecordHighTemp();
void getRecordLowYear();
void getRecordLowTemp();
 

void main()
{
 
 cout << "\n\n  Temperature Statistics [1]";
 getMonths();
}

 
void getMonths()
{
 string str[11];
 string line;
 ifstream months;
 
 months.open("MonthsA.txt");
 
 if(!months) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
  while(months.good())
    {
  for (int i = 0; i < 12; i++)
  {
   getline (months,line);                
   str[i] = line;       
  }
  
  cout << "\n\n" << "  Months ";
    
  for (int i = 0; i < 12;i++)
  {
   cout << "  " << str[i];
  }
  }
  months.close();
  cout << "\n\n" << "  Average";
  getHighs();
}
 
 

void getHighs()
{
 ifstream numberFileH;
 int a,b,c,d,e,f,g,h,i,j,k,l;
 int num[11];
 numberFileH.open("Highs.Dat");
 
 if(!numberFileH) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
    
 while (numberFileH.good())
 {
  numberFileH >> a >> b >> c >> d >> e >> f >> g >> h >> i >> j >> k >> l;
 }
 num[0] = a;
 num[1] = b;
 num[2] = c;
 num[3] = d;
 num[4] = e;
 num[5] = f;
 num[6] = g;
 num[7] = h;
 num[8] = i;
 num[9] = j;
 num[10] = k;
 num[11] = l;
 
 cout << "\n  Highs     ";
 
 for (int z = 0; z < 12; z++)
 {
  cout << num[z] << "   ";
 } 
 numberFileH.close();
 getLowTemps();
}
 
 

void getLowTemps()
{
 ifstream numberFileL;
 int m,n,o,p,q,r,s,t,u,v,w,x;
 int numL[11];
 numberFileL.open("Lows.pink");
 
 if(!numberFileL) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
    
 while (numberFileL.good())
 {
  numberFileL >> m >> n >> o >> p >> q >> r >> s >> t >> u >> v >> w >> x;
 }
 numL[0] = m;
 numL[1] = n;
 numL[2] = o;
 numL[3] = p;
 numL[4] = q;
 numL[5] = r;
 numL[6] = s;
 numL[7] = t;
 numL[8] = u;
 numL[9] = v;
 numL[10] = w;
 numL[11] = x;
 cout << "\n  Lows      ";
 
 for (int z = 0; z < 12; z++)
 {
  cout << numL[z] << "   ";
 } 
 numberFileL.close();
 getPrecipitation();
}
  
 
 
void getPrecipitation()
{
 ifstream numberFileP;
 double m,n,o,p,q,r,s,t,u,v,w,x;
 double numL[12];
 numberFileP.open("Precipitation.wet");
 
 if(!numberFileP) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
    
 while (numberFileP.good())
 {
  numberFileP >> m >> n >> o >> p >> q >> r >> s >> t >> u >> v >> w >> x;
 }
 numL[0] = m;
 numL[1] = n;
 numL[2] = o;
 numL[3] = p;
 numL[4] = q;
 numL[5] = r;
 numL[6] = s;
 numL[7] = t;
 numL[8] = u;
 numL[9] = v;
 numL[10] = w;
 numL[11] = x;
 cout << "\n\n  Rainfall      \n  Inches  ";
 
 for (int z = 0; z < 12; z++)
 {
  cout << numL[z] << " ";
 } 
 numberFileP.close();
 getRecordHighYear();
}
 
 
 

void getRecordHighYear()
{
 string str[1];
 string line;
 ifstream highY;
 highY.open("RecordHighYear.txt");
 
 if(!highY) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
  while(highY.good())
    {
  getline (highY,line);                
  str[0] = line;       
 }
  
  cout << "\n\n" << "  Records \n" << "  Year    ";
  cout << str[0] << endl;
 
  highY.close();
  getRecordHighTemp();
}
 
 

void getRecordHighTemp()
{
 ifstream numberFileL;
 int m,n,o,p,q,r,s,t,u,v,w,x;
 int numL[12];
 numberFileL.open("RecordHighTemperature.txt");
 
 if(!numberFileL) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
    
 while (numberFileL.good())
 {
  numberFileL >> m >> n >> o >> p >> q >> r >> s >> t >> u >> v >> w >> x;
 }
 numL[0] = m;
 numL[1] = n;
 numL[2] = o;
 numL[3] = p;
 numL[4] = q;
 numL[5] = r;
 numL[6] = s;
 numL[7] = t;
 numL[8] = u;
 numL[9] = v;
 numL[10] = w;
 numL[11] = x;
 cout << "  High   ";
 
 for (int z = 0; z < 12; z++)
 {
  if (numL[z] < 100)
  {
  cout << "   " << numL[z];
  }
  else if (numL[z] > 99)
  {
  
   cout << "  " << numL[z];
  }
 }
 numberFileL.close();
 getRecordLowYear();
}
 
 

void getRecordLowYear()
{
 string str[11];
 string line;
 ifstream lowY;
 lowY.open("RecordLowYear.txt");
 
 if(!lowY) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
 while (lowY.good())
 {
  for (int i = 0; i < 12; i++)
  {
   getline (lowY,line);                
   str[i] = line;       
  }
 } 
 cout << "\n\n  Year    ";
 for (int z = 0; z < 12;z++)
 {
 
  cout << str[z] << " ";
 }
  lowY.close();
  getRecordLowTemp();
}
 
 
 

void getRecordLowTemp()
{
 ifstream numberFileL;
 int m,n,o,p,q,r,s,t,u,v,w,x;
 int numL[12];
 numberFileL.open("RecordLowTemperature.txt");
 
 if(!numberFileL) 
 {
  cout << "  Error opening file" << endl;
        _getch();        
 }
    
 while (numberFileL.good())
 {
  numberFileL >> m >> n >> o >> p >> q >> r >> s >> t >> u >> v >> w >> x;
 }
 numL[0] = m;
 numL[1] = n;
 numL[2] = o;
 numL[3] = p;
 numL[4] = q;
 numL[5] = r;
 numL[6] = s;
 numL[7] = t;
 numL[8] = u;
 numL[9] = v;
 numL[10] = w;
 numL[11] = x;
 cout << "\n  Low      ";
 
 for (int z = 0; z < 9; z++)
 {
  if (numL[z] < 0)
  {
  cout << numL[z] << "  ";
  }
  else if (numL[z] < 10 && numL[z] > -1)
  {
   cout << "  " << numL[z];
  }
  else if (numL[z] > 9)
  {
   cout << "   " << numL[z];
  }
 }
 for (int z = 9; z < 12;z++)
 {
  if (numL[z] > 0)
  {
  cout << "    " << numL[z];
  }
  else if (numL[z] < 0)
  {
   cout << "  " << numL[z];
  }
 }
 numberFileL.close();
 cout << "\n\n\n" << endl;
 cout << "  [1]  Omaha weather statistics, Accessed 5/7/2012\n";
 cout << "       [URL]http://www.weather.com/weather/wxclimatology/monthly/USNE0363[/URL]" << endl;
 getClosing( );
 _getch();
}
 
Back