hey guys. i'm working on this project and i can't seem to get it figured out. there's a lot of steps involved, but i can't get any further right now because i cannot seem to figure out how to place values in an array.

the program should ask the user for a file name, open the file, and then put all the data from the file into an array, to start with.

the sample text file i've been given is called "input.txt" and consists of both letters and numbers:

Nut 10  1.99    0.53
Bolt    10  1.49    0.42
Screw   10  1.79    0.39

my code so far is this...
there are functions i have yet to write and variables i have yet to make use of, but you get the picture. the program itself compiles, but when i debug the values in ARRAY are something like "nut//////" and a bunch of random characters. any idea what i'm doing wrong??

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void SortName(char[]); // sorts list by item name
void SortProfit(char[]); // sorts list by profit
void SortPrice(char[]); // sorts list by price
void Buy (char[]); // adds one to a specific item chosen by user
void Display (char[]); // displays list as-is
void FindProfit (int,int,int); //finds profit of items

int main ()

{
    char choice; //to hold user's menu choice
    char item[81]; //to hold item user wishes to buy
    char file[81];
    char item_name[81];
    const int NUM_ROWS = 3;
    const int NUM_COLS = 4;
    char ARRAY[NUM_ROWS][NUM_COLS];
    unsigned int number_sold;
    int ROWS, COLS;
    float sales_price;
    float item_cost;

    ifstream ifile;

    cout << "Enter the file name: ";
    cin >> file;

    ifile.open(file); //opens file

    for (ROWS = 0; ROWS < NUM_ROWS; ROWS++)
    {
        for (COLS = 0; COLS < NUM_COLS; COLS++)
            ifile >> ARRAY[ROWS][COLS]; // trying to input text into the array
    }

    if(!ifile) //to account for mistyped file name
    {
        cout <<"Could not open the file." << endl;
        return 0;
    }

return 0;
}

Recommended Answers

All 6 Replies

#include <fstream>
#include <iostream>

using namespace std;

int main ()
  {
    ifstream in("asd.rff");       //containing   "1,2,3,4,5"
    char* str;
    char* things[4][10];
    in.getline(str,40,'\0');

    things[0][10]=strtok(str,",");
    for (int x=1;x<4;x++)   
      things[x][10]=strtok(NULL,",");
      
  }

1) Check to see if the file opened before you try to use it.
2)This:
char ARRAY[NUM_ROWS][NUM_COLS];
could be used to hold individual characters or null terminated char arrays, ie C style strings. And this:
ifile >> ARRAY[ROWS][COLS];
reads a single char at a time into ARRAY at whatever ROWS and COLS refer to. This is fine, as far as it goes. You need to be sure that you have some way to terminate input though, otherwise it won't stop input after it reads in nut/bolt/screw, it will keep right on going until it has read in enough char to fill the array, whether the char are alphabetical, digits or whitespace. To use this input as a string later on you would need to be sure you null terminate each line, too. I think you would do better changing NUM_COLS to something like 15, or something to hold the longest word you might encounter plus 1 for the null terminating char, and then you could read in the first word in each line by doing this:
ifile >> ARRAY[ROWS];
and it will automatically null terminate the word, stopping input with the first whitespace char it finds, whether that is a space or a newline char, or whatever.
3) You want to change sales_price and item_cost to arrays as well and I think you want to have a fourth array named something like num_items.
4) Then with a series of calls to >> you can read the file contents one line at a time using the >> operator, storing each input in the appropriate array.
5) Realize the list in programming carries a specific conotation, and it doesn't refer to an array. It is a little more involved to sort a list than it is to sort an array, but it isn't impossible.

i'm sorry... i'm pretty new at this, this is my first semester, and it's killing me. i don't really understand your above post, Lerner. thank you for your input. I'm not entirely sure where I should go from here.

I realized from your post that I do want 4 seperate arrays instead of one 2-d array... correct?

the end result of the project is to get a filename, and sort whatever is in the file in different orders.

i am sure we are supposed to have use arrays, it just seems so much harder than it really is, I'm sure.

I changed my code around a little bit, but I'm still not sure what to do.

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void SortName(char[]); // sorts list by item name
void SortProfit(char[]); // sorts list by profit
void SortPrice(char[]); // sorts list by price
void Buy (char[]); // adds one to a specific item chosen by user
void Display (char[]); // displays list as-is
void FindProfit (int,int,int); //finds profit of items

int main ()

{
char choice; //to hold user's menu choice
char item[81]; //to hold item user wishes to buy
char file[81]; //user inputted file name
char item_name[20];//array to hold item names
unsigned int number_sold[10];//array to hold list of number of items sold
float sales_price[10]; //array to hold sale prices
float item_cost[10]; //aray to hold cost of items
char temp;


ifstream ifile;

cout << "Enter the file name: ";
cin >> file;

ifile.open(file); //opens file

if(!ifile) //to account for mistyped file name
{
cout <<"Could not open the file." << endl;
return 0;
}


cout << endl;
cout <<"Sort by (N)ame" <<endl;
cout << "Sort by total (P)rofit" <<endl;
cout << "Sort by item (S)ales Price" <<endl;
cout << "(D)isplay inventory" <<endl;
cout << "(B)uy an item" <<endl;
cout << "(Q)uit program" <<endl <<endl;

cout << "Please enter your choice: " ;
cin >> choice;


if ( (choice=='Q') || (choice=='q') ) //case insensitive choices
{
cout << "You have choisen to quit." <<endl;
return 1; //exits program
}

if ( (choice=='N') || (choice=='n') ) //case insensitive choices
{
void SortName(char list[]);
}

if ( (choice=='P') || (choice=='p') ) //case insensitive choices
{
void SortProfit(char list[]);
}

if ( (choice=='S') || (choice=='s') ) //case insensitive choices
{
void SortSales(char list[]);
}

if ( (choice=='D') || (choice=='d') ) //case insensitive choices
{
cout << " Product Name #Sold Sales Price Item Cost Total Profit" <<endl;
cout << "=========================================================================" <<endl;
void Display (char list[]);

}

if ( (choice=='B') || (choice=='b') ) //case insensitive choices
{
cout << "Please enter the item name you wish to buy: ";
cin >> item;
void Buy (char list[]);

}

return 0;

}

void SortName (char list[])
{

}

void SortProfit (char list[])
{

}

void SortSales (char list[])
{

}

void Display (char list[])
{

}

void Buy (char list[])
{

}

void FindProfit (int sales_price,int item_cost,int number_sold)
{

}

sorry, i'm just grasping at straws here as i'm trying to finish this project as it's due in 24 hours. i did not think it would be this difficult.

one question i have, is it possible to read in a row at a time and assign each row to its own array? such as a list of items, prices, etc?

Unfortunately this board eats up indenting/formatting you use when writing code making it very hard to read. You can retain the formatting by enclosing your code in code tags. The process of doing so is explained in the announcement section of this board. Not doing so frequently means that many of the most knowledgeable responders won't respond to your post.

>>is it possible to read in a row at a time and assign each row to its own array

As long as the array the row is assigned to is of type string, yes you can read an entire row/line from your file at once and store it in an array. In fact, that's what caut_baia does in the code posted. It is difficult, if not impossible, to search the row/line itself for a given field when the fields are stored as a single string, so the input string can be broken up (parsed) into separate fields which can then be stored in the appropriate arrays (or member variables when you start using object oriented programming). The use of strtok() to parse a line is demonstrated by caut_baia, but parsing can also be done with stringstreams if you are familiar with them. As a third alternative, parsing input directly when reading from file sometimes works. That's what I talked about. The file content you posted has the fields separated by whitespace so >> can be use to parse the input into the separate arrays directly when they are read in.

Using parallel arrays seems reasonable since the file is known to be of a fixed number of lines .

const int MaxLines = 3;
const int MaxLength = 15;  //each item name can be up to 14 char in length
char item_names[MaxLines][MaxLength];
int num_remaining[MaxLines];
int aquisition_cost[MaxLines];
int sales_price[MaxLines];  

for(int i = 0; i < MaxLines; ++i)
{
    infile >> item_names[i] >> num_remaining[i] >> sales_price[i] >> aquisition_cost[i];
}

That should read a 3 line file of format posted previously, putting all data read in in the appropriate arrays. Each item's data is accessed using the index value of the desired element. So item_name[x] has aquisition_cost[x] and sales_price[x], etc. You want to have each item's attributes remain the same whether you sort by the items by item_name or aquisition_cost or sales_price, etc, so pass all the arrays to whatever function is doing the sorting and whenever you swap two elements in one array you swap the same elements in each array passed to the function(s).

thank you! and thank you for the posting advice, i should have read that first.

what i have now based on your suggestion, which makes sense for the most part to my untrained mind, successfully reads in the first line into each seperate array, but not any following lines.

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void SortName(char[]); // sorts list by item name
void SortProfit(char[]); // sorts list by profit
void SortPrice(char[]); // sorts list by price
void Buy (char[]); // adds one to a specific item chosen by user
void Display (char[]); // displays list as-is
void FindProfit (int,int,int); //finds profit of items

int main ()

{
	const int MaxLines = 3;
	const int MaxLength = 15;  //each item name can be up to 14 char in length
	char choice; //to hold user's menu choice
	char item[81]; //to hold item user wishes to buy
	char file[81]; //user inputted file name
	char item_name[MaxLines][MaxLength];//array to hold item names
	unsigned int number_sold[10];//array to hold list of number of items sold
	float sales_price[10]; //array to hold sale prices
	float item_cost[10]; //aray to hold cost of items
	

    ifstream ifile;

    cout << "Enter the file name: ";
    cin >> file;

    ifile.open(file); //opens file
	


	for(int i = 0; i < MaxLines; i++)
	{
		ifile >> item_name[i] >> number_sold[i] >> sales_price[i] >> item_cost[i];
	}


    if(!ifile) //to account for mistyped file name
    {
		cout <<"Could not open the file." << endl;
        return 0;
    }

	 


	cout << endl;
	cout <<"Sort by (N)ame" <<endl;
	cout << "Sort by total (P)rofit" <<endl;
	cout << "Sort by item (S)ales Price" <<endl;
	cout << "(D)isplay inventory" <<endl;
	cout << "(B)uy an item" <<endl;
	cout << "(Q)uit program" <<endl <<endl;

	cout << "Please enter your choice: " ;
	cin >> choice;

	


	if ( (choice=='Q') || (choice=='q') ) //case insensitive choices
	{
		cout << "You have choisen to quit." <<endl;
		return 1; //exits program
	}

	if ( (choice=='N') || (choice=='n') ) //case insensitive choices
	{
		void SortName(char list[]);
	}

	if ( (choice=='P') || (choice=='p') ) //case insensitive choices
	{
		void SortProfit(char list[]);
	}

	if ( (choice=='S') || (choice=='s') ) //case insensitive choices
	{
		void SortSales(char list[]);
	}

	if ( (choice=='D') || (choice=='d') ) //case insensitive choices
	{
		cout << "  Product Name     #Sold     Sales Price     Item Cost     Total Profit" <<endl;
		cout << "=========================================================================" <<endl;
		void Display (char list[]);
		
	}

	if ( (choice=='B') || (choice=='b') ) //case insensitive choices
	{
		cout << "Please enter the item name you wish to buy: ";
		cin >> item;
		void Buy (char list[]);
		
	}

	

	return 0;

}

void SortName (char list[])
{

}

void SortProfit (char list[])
{

}

void SortSales (char list[])
{

}

void Display (char list[])
{
	
}

void Buy (char list[])
{

}

void FindProfit (int sales_price,int item_cost,int number_sold)
{

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.