Jump to content

Request for a Small Bit of Code


CoolMaster

Recommended Posts

You don't need anything that advanced to make this work. Doing a `file >> variable;` separates by whitespace, so there's really no need to do it yourself going by line then separating (though I probably would, but lets keep it simple for the guy).

 

Your problem is simply an end of file problem. All you should have to do to get this working properly is add a .eof() in the while loop, for example:

 

	while(!filein.eof())
{
	// do stuff
}

Share this post


Link to post
Share on other sites

Your problem is simply an end of file problem. All you should have to do to get this working properly is add a .eof() in the while loop, for example:

 

	while(!filein.eof())
	 {
		 // do stuff
	 }

Haha! So I do know what I'm talking about! ...even if I did make it needlessly complicated... :lol:

Share this post


Link to post
Share on other sites

lol ;)

woot it worked,HAha thx guys, btw i may need ur help later on, it was a whole summer of no c++ so it's gonna be a long endeavor

:-D as long as thats alrighty with yall

 

I guess mine was continuing to loop down at the last one because the file is still there, this makes sure it stops :-D

 

 

EDIT: now for the hard part, suppose I wanted to put it in a Struct? how would I go about that, i checked c++ but our teacher is dumb and he only went over structs for like 20 min even tho it's like an integral part of this program, because if I can get it in a struct. then sorting it shouldnt be a big deal it'd just be editing the struct.

 

he never said he wanted these in functions I dont believe but if they have to be a function that shouldnt be a big deal right (i suck with functions)

Edited by CoolMaster

Share this post


Link to post
Share on other sites

Functions aren't that bad. They actually make life a lot easier once you start doing more complex programs.

 

If you can put the data into an array, then you can put data into a struct. What specifically are you having problems with? Here's a link that might be helpful.

Share this post


Link to post
Share on other sites

ya I was looking at that

I need to put this

Dell 512 1.7 120

HP 1024 1.4 80

Compaq 1024 1.6 100

Toshiba 512 1.8 160

Lenovo 2048 1.5 120

 

into a struct that can be changed and arranged depending on how they want it organized (by name,cpu speed etc.)

 

i was thinking like

struct stuff{

string name;

int memory,hd;

float cpu;

}

for like the basics for it, then i'd need some kinda Loop to store it all

but it seems like it'd need to be something like struct stuff

with i = 0 but it wont let me do that?

so i could have like multiple structs....i mean i'd have to wouldnt I for each computer because I cant store that all into one struct that can be organized can I?

 

what about something like this

struct stuff{

string name;

int memory,hd;

float cpu;

} computer [MAX]

 

 

 

for (n=0; n<MAX; n++)

{

filein >> brand

stuff.name = brand;

filein >> memory

stuff.mem = memory;

filein >> proc

stuff.cpu = proc

 

 

}

 

would that work....or no

 

thanks for the help guys I will get through this :_D

Share this post


Link to post
Share on other sites

but it seems like it'd need to be something like struct stuff

with i = 0 but it wont let me do that?

so i could have like multiple structs....i mean i'd have to wouldnt I for each computer because I cant store that all into one struct that can be organized can I?

 

what about something like this

struct stuff{

string name;

int memory,hd;

float cpu;

} computer [MAX]

For structs, you need to use the "computer.name" method to get to the data. And you can't really specify a max length for a struct either. Any struct you create will have all the variables, even if they're null, 0, or empty strings and nothing more. In this situation, a struct will only hold the data for a single computer. This is where arrays come in handy. You can store all of the structs in an array and access all the data.

 

for (n=0; n<MAX; n++)

{

filein >> brand

stuff.name = brand;

filein >> memory

stuff.mem = memory;

filein >> proc

stuff.cpu = proc

 

 

}

 

would that work....or no

 

thanks for the help guys I will get through this :_D

What I would do here is use the while loop you had before (with the filein.eof() ), read the variables into a struct (like you did here, although you could just say "filein >> stuff.cpu"), then store the struct in an array. Use a counter to move to the next spot in the array.

 

That's probably not 100% clear, so just let me know what needs clarification.

Share this post


Link to post
Share on other sites

ummm hmm, im a bit confused on ur loop part. i dont know how I would read it until multiple arrays?

 

perhaps an example code?

 

because for this program we have to use arrays and structs (dont ask me why but we gotta)

i wish his website would be up so i could show u what I meant, plus remember these have to be reorganized compared to what the user picks? so i hope thats ok

 

 

EDIT: http://www.cs.uky.edu/~ryan/CS215_F07/programs/1pgm.html

 

theirs actually what he's asking for if that helps

apparently i need to make an array of computer structs? large enuff for 10 computers,.........how?

 

and to make each one of these a function im a bit confused on because I dont know how to change them to functions very well.

Edited by CoolMaster

Share this post


Link to post
Share on other sites

I'm a bit rusty when it comes to actual coding, but I'll give it a try. :)

 

   int counter;
  string computers[];
  struct computer {
   string name;
   int mem;
   float cpu;
  };

  computer temp;	  //initialize temp struct
  counter = 0

//read data from file, store in struct, store struct in array
  while(!filein.eof()) {
   filein >> temp.name;
   filein >> temp.mem;
   filein >> temp.cpu;

   computers[counter] = stuff;

   counter++;
  };

  //To make sure the previous loop works correctly,
  //make another for-loop that spits out the data
  //stored in the array/structs

  //I'm not sure if this is the correct way to do it,
  //but try it anyway
  for( i=0; i<computer.length; i++) {
   cout << computers[i].name << ' ' << computers[i].mem << ' ' << computers[i].cpu << endl;
  };

  //if that doesn't work, put the values into a
  //temporary struct, then do the output

 

Does that help?

 

EDIT: All of this makes me wish I could have enrolled in that C++ class this semester. :lol: Dang work. <_<

Edited by Bleeble

Share this post


Link to post
Share on other sites

k here's what I got.....it seems to work, now i need to store the structs into arrays to be able to store them

 

//* Matt Smith
  Section <>
  CS 215  */





#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "progrm1head.h"

using namespace std;






int main ()
{   
string brand;
int memory,hard;
float proc;

string compname[MAX];
float cpuspec[MAX];
int mem[MAX];
int HDD[MAX];
ifstream filein,fileopen;
string filename;
int ordertype;

cout << "Please enter a file name containing your list of computers: ";
cin >> filename;
filein.open(filename.c_str());
cout << endl << endl;
cout << "Please choose a parameter to sort computers by (ascending): " << endl;
cout << "		1.Brand 2.Memory" << endl;
cout << "		1.CPU   2.Storage" << endl;
cin >> ordertype;



int ct = 0;
 while(!filein.eof())
 {

  filein >> brand;
  compname[ct] = brand;

  filein >> memory;
  mem[ct] = memory;

  filein >> proc;
  cpuspec[ct] = proc;

  filein >> hard;
  HDD[ct] = hard;


cout << compname[ct] << " "; 
cout << mem[ct] << " ";
cout << cpuspec[ct] << " ";
cout << HDD[ct] << " ";
cout << endl;



  ct ++;

 }

 filein.close();

fileopen.open(filename.c_str());

int ct2 = 0;
 while(!fileopen.eof())
 {
fileopen >>  COMPS[ct2].name;
fileopen >>  COMPS[ct2].mem;
fileopen >>  COMPS[ct2].cpu;
fileopen >>  COMPS[ct2].hd;



ct2++;

 }




return 0;
}

 

 

basically all I did was store the part into an array and a struct i dunno if thats cheating but....i dont know what else to try?

Edited by CoolMaster

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...