Input data files/loops.

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1947 Views - Last Post: 15 October 2010 - 09:24 PM Rate Topic: -----

Topic Sponsor:

#1 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 24-September 10

Input data files/loops.

Posted 15 October 2010 - 01:10 PM

Hey everyone, I'm a bit confused with a few things. I'm doing this lab/tutorial project where I read data from a text file that has 20 temperatures for 10 cities (a min and max for each city) along with their province that they're in. I've figured out how to read and display data from the file, but I'm unsure how to modify/re-arrange the data. Here's what I mean, this is just a very short example, it's nothing close to the finished product:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(){
ifstream inFile;
const int SIZE = 81;
char name[SIZE];

inFile.open ("mycities.txt");

inFile >> name; //1st word
cout << name;

inFile >> name;   // 2nd word 
cout << setw(4) << name;

inFile >> name; //3rd word etc.
cout << name;

inFile.close();
cout << "Done";
system ("PAUSE");
return 0;
}


The text file as of right now doesn't have the temperatures ordered numerically from coldest to hottest, which is what I have to do. Basically he says we should use a while loop to do this, but I'm unsure how to specify which "name" to use if they're all called "name". How do I specify which word/number I want to arrange where when they're all called name?

I'm also confused as to what he means here :Use constants to define the size of the arrays and the max number of characters in a city/province name.
Specify the name arrays as char[SIZE][NAME_LENGTH], where SIZE is the number of cities (10 in our case) and NAME_LENGTH is a constant, set it to 64.
Use a temporary variable to swap elements in the array, in 2 steps …
To copy strings either:
Use a while loop to copy all elements up-to and including the terminating null character ‘\0’; or Use strcpy(dst, src) function and include <string.h> , recommended.

This is kind of confusing to me, does anyone know a good basic tutorial for arrays?
Thanks.

This post has been edited by Munchiester: 15 October 2010 - 01:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Input data files/loops.

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 1876
  • View blog
  • Posts: 5,652
  • Joined: 25-December 09

Re: Input data files/loops.

Posted 15 October 2010 - 01:35 PM

Search this forums Tutorials on Arrays or this link Arrays

Jim

This post has been edited by jimblumberg: 15 October 2010 - 01:36 PM

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is online

  • No Sugar Coding Here!
  • member icon

Reputation: 4673
  • View blog
  • Posts: 20,340
  • Joined: 23-August 08

Re: Input data files/loops.

Posted 15 October 2010 - 02:16 PM

This tutorial might be helpful as well
Was This Post Helpful? 1
  • +
  • -

#4 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 24-September 10

Re: Input data files/loops.

Posted 15 October 2010 - 02:48 PM

Okay and do you know how I can specify 1 name out of all in my txt file?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon

Reputation: 1876
  • View blog
  • Posts: 5,652
  • Joined: 25-December 09

Re: Input data files/loops.

Posted 15 October 2010 - 02:52 PM

Quote

Okay and do you know how I can specify 1 name out of all in my txt file?


Quote

Specify the name arrays as char[SIZE][NAME_LENGTH], where SIZE is the number of cities (10 in our case) and NAME_LENGTH is a constant, set it to 64.


Maybe?
const int SIZE = 10;
const int NAME_LENGTH = 64;

name[SIZE][NAME_LENGTH];


Jim

This post has been edited by jimblumberg: 15 October 2010 - 02:55 PM

Was This Post Helpful? 1
  • +
  • -

#6 #define  Icon User is offline

  • Programmer
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,084
  • Joined: 19-February 09

Re: Input data files/loops.

Posted 15 October 2010 - 03:37 PM

View PostMunchiester, on 15 October 2010 - 10:48 PM, said:

Okay and do you know how I can specify 1 name out of all in my txt file?


Do you mean, how can you access one name in the array.

     
#include <iostream>

using namespace std;

int main()
{
  char name[10][81];

  cout << "Enter name: ";
  cin >> name[0];

  cout << "Name is " << name[0];

  cin.sync();
  cin.get();
}




Output:
Enter name: Munchiester
Name is Munchiester
Was This Post Helpful? 1
  • +
  • -

#7 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 24-September 10

Re: Input data files/loops.

Posted 15 October 2010 - 03:58 PM

Kind of, I don't think I'm going to be using cin in my program though, as it's simply just suppose to display the data.
Posted Image

That's what it's suppose to look like, I'm not supposed to input anything, I just need to know how to like reference back to certain numbers so I can rearrange them using the while loop I think.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon

Reputation: 1876
  • View blog
  • Posts: 5,652
  • Joined: 25-December 09

Re: Input data files/loops.

Posted 15 October 2010 - 04:12 PM

I would use getline() to get a line of text from the file then use stringstream to process each line.

There are several good file input/output tutorials in the tutorial section of this Forum. Try Basic C++ File IO to start.

If you are taking a course in school your TEXTBOOK should also have at least one chapter on basic file io.


Jim
Was This Post Helpful? 1
  • +
  • -

#9 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 24-September 10

Re: Input data files/loops.

Posted 15 October 2010 - 04:29 PM

Okay, also we don't have textbooks, it's actually quite ridiculous. He prepares about 20 slides, which tell us almost nothing, which is why I probably come across as a dumbass in every single thread I've posted on this forum =/. Even paying attention 100% of the time in class isn't enough, everyone is really confused.

So to move on, say I want get the line "Yellowknife NWT -31 -23". I would just be able to use getline, then re-arrange all the lines according to the temperatures from coldest to hottest using a while loop?
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Ayman El-Sawah*


Reputation:

Re: Input data files/loops.

Posted 15 October 2010 - 04:44 PM

*
POPULAR

Actually you do have textbooks. I do prepare 20 slides, with examples taken out of the textbook you have. Now I don't know why you aren't paying attention in my class but I assume you want to pass this year. So try to start okay?
Was This Post Helpful? 7

#11 jimblumberg  Icon User is online

  • member icon

Reputation: 1876
  • View blog
  • Posts: 5,652
  • Joined: 25-December 09

Re: Input data files/loops.

Posted 15 October 2010 - 04:49 PM

You will need to extract the temperature from the lines, convert to an integer, then sort.

So now it's time to start writing some code.

You might also want to check out this book Bruce Eckel Thinking in C.

Jim
Was This Post Helpful? 2
  • +
  • -

#12 Munchiester  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 24-September 10

Re: Input data files/loops.

Posted 15 October 2010 - 05:29 PM

Alright I handed it in, I think I got it right for the most part, thanks everyone.
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is online

  • No Sugar Coding Here!
  • member icon

Reputation: 4673
  • View blog
  • Posts: 20,340
  • Joined: 23-August 08

Re: Input data files/loops.

Posted 15 October 2010 - 05:35 PM

Oh my...BUSTED. Welcome, Dr. El-Saweh. I'm sure this won't be the last of your students you'll find here, should you venture through again.
Was This Post Helpful? 0
  • +
  • -

#14 elgose  Icon User is offline

  • D.I.C Head

Reputation: 95
  • View blog
  • Posts: 221
  • Joined: 03-December 09

Re: Input data files/loops.

Posted 15 October 2010 - 07:11 PM

Oh dear... I think I cried a little from laughing. $5 says the Prof matches code from this/previous threads and knows who he is?

I think this is a great example of why it's sometimes a good idea to post your question with a made up/generic program that only contains the area you're having issues in. Keeps you focused on being able to locate exactly what you're having issues with, and (especially when for a class), from being able to be identified.

And, jimblumberg, thanks for that link - I think it actually looks really interesting, I'm gonna try to whip through the first chapter tonight.

This post has been edited by elgose: 15 October 2010 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#15 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 145
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Input data files/loops.

Posted 15 October 2010 - 07:17 PM

View PostAyman El-Sawah, on 15 October 2010 - 05:44 PM, said:

Actually you do have textbooks. I do prepare 20 slides, with examples taken out of the textbook you have. Now I don't know why you aren't paying attention in my class but I assume you want to pass this year. So try to start okay?

This has got to be the funniest thing I've seen all day.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2