hi im trying to read a txt file and could you please check what is wrong with this?

this is my code

// This program will calculate the charges for shipping
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
using std :: ifstream;
using namespace std;
int main()
{ // curly #1
    char choice; //choices available in the main menu.
    float weight; // Weight to be entered.
    int weightcategory;
    int shippingzone; // Shipping Zone to be selected.
    int customeridno; // Customer ID Number.
    int zone;
    int charge;
      
    cout << "Welcome to the DOWHILE FREIGHT ordering cart" << endl;
    cout << "-------------MENU--------------" << endl;
    cout << "1. To begin ordering enter '1'"  << endl;
    cout << "2. To quit enter '2'" << endl;
    cout << "-------------------------------" << endl;
    cout << "Choose an option (enter number):" << endl;
    cin >> choice;
    
    if(choice == '1')
    {   // curly #2      
              customeridno = rand() * rand();
              cout << "----------------------------------------" << endl;
              cout << "Your Customer ID number is " << customeridno << endl;
              cout << "----------------------------------------" << endl;
              cout << "Please Enter the weight of your item" << endl; // Enter the weight of your item
              cout << "We ship items between 1 to 60KG" << endl;
              cin >> weight;
              
              do 
              { // curly #3
              if(weight >= 1 && weight <= 60)
              { // curly #4
                        if (weight >= 1 && weight < 10)
                        weightcategory = 0;
                        else if (weight >= 10 && weight < 20)
                        weightcategory = 1;
                        else if (weight >= 20 && weight < 30)
                        weightcategory = 2;
                        else if (weight >= 30 && weight < 40)
                        weightcategory = 3;
                        else if (weight >= 40 && weight < 50)
                        weightcategory = 4;
                        else if (weight >= 50 && weight <= 60)
                        weightcategory = 5;
                        
                        cout << "You have entered " <<weight<< "KG" << endl;
                        cout << "We are able to ship that amount of weight" << endl;
                        cout << "-----------------------------------------" << endl;
                        cout << "Please Enter the shipping number zone you would like to use" << endl;
                        cout << "We have 4 Shipping zones." << endl;
                        cout << "1. Standard Shipping. " << endl; 
                        cout << "2. Special Standard Shipping." << endl;
                        cout << "3. Express Shipping." << endl;
                        cout << "4. Express Special Shipping." << endl;
                        cout << "-----------------------------------------" << endl;
                        cout << "Choose an Option (enter number):" << endl; // Select the Shipping Zone
                        cin >> shippingzone;
                        
                        do
                        { // curly #5
                        if (shippingzone >=1 && shippingzone <=4)
                        { // curly #6
                                         cout << "You have Selected Shipping Zone " << shippingzone << endl;
                                         zone = shippingzone - 1;
                                         ifstream.charge("shippingcharges.txt"); {
                                                         if(!charge) {
                                                                     cout << "cannot open file" << endl;
                                                                     return (1);
                                                                     }
                                                                     
                                                                     while (!charge.eof()&&(weightcategory<7)
                                                                     charge >> shippingcharges [weightcategory][0];
                                                                     }
                                                                     charges.close();
                                         
                                                                             
                                                                                    
                                         
                                         
                                         } // curly 6
                        else
                        cout << "ERROR - Please enter one of the shipping zones shown above" << endl;
                        cin >> shippingzone;
                        } while (shippingzone >= 1 && shippingzone <= 4); // curly 5
              } // curly #4
              else
              cout << "ERROR - Please Enter weight between 1 to 60KG" << endl;
              cin >> weight;
              } while (choice == '1'); // curly 3
    } // curly #2
    if (choice == '2')
    {
               exit (1);
               }
    else
    cout << "ERROR - Please choose one of the following options" << endl;
    cin >> choice;
    
       
system ("pause");
return 0;
} // curly #1

this is what i need help with

ifstream.charge("shippingcharges.txt"); {
                                                         if(!charge) {
                                                                     cout << "cannot open file" << endl;
                                                                     return (1);
                                                                     }
                                                                     
                                                                     while (!charge.eof()&&(weightcategory<7)
                                                                     charge >> shippingcharges [weightcategory][0];
                                                                     }
                                                                     charges.close();

i got an error message saying

"expected primary-expression before '.' token"


could you please show me how to read txt files

Use your >> statement to drive the loop:

while(weightcategory<7 && charge >>shippingcharges[weightcategory][0]);

(since it's a one line statement put the ; after it, but if there's a body to the while don't put that one in)
That avoids having to use the eof at all, which can cause other problems like reading in the last line twice.
You could even do something like test for the weightcategory before you go into the while and take that part out of the test condition, since weightcategory doesn't change in that loop.

Use your >> statement to drive the loop:

while(weightcategory<7 && charge >>shippingcharges[weightcategory][0]);

(since it's a one line statement put the ; after it, but if there's a body to the while don't put that one in)
That avoids having to use the eof at all, which can cause other problems like reading in the last line twice.
You could even do something like test for the weightcategory before you go into the while and take that part out of the test condition, since weightcategory doesn't change in that loop.

hey man, i seriously dont get this

do you have the code for opening txt files

=/

Substitute what I wrote for lines 7-9 in your snippet.

Keep in mind it's only reading into one variable so you'll probably get the last value in the file. If you have more things you want to read in, you'll need to read it into an array. If that's the case, create the array and use the while loop I gave you to read into it, but add in an index value which you increment in the body of the loop.

Edit: and if you want to read the "shippingzone"th value just put a loop counter where your weightcategory variable was:

int zonecount = 0;
while(zonecount < shippingzone && charge >> shippingcharges[weightcategory][0])
{
    zonecount++;
}

Edited 6 Years Ago by jonsca: n/a

ifstream.charge("shippingcharges.txt");

Replace the dot with a space.

ifstream charge("shippingcharges.txt");
Comments
You found the right period

Oh geez. I glazed right over the period. My apologies OP. When you cited the error I thought you meant the .eof part.

Figure out what you need to read in from the file and when (e.g., if the shipping zone is 5 I need the second row, third column or whatever)

Oh geez. I glazed right over the period. My apologies OP. When you cited the error I thought you meant the .eof part.

Figure out what you need to read in from the file and when (e.g., if the shipping zone is 5 I need the second row, third column or whatever)

customers can choose whatever shipping zone they want from 1 - 4
depending on there weight

Weight		1	2	3	4
1-10KG		2.5	3.5	4	5
10-20KG		3.5	4	5	6.5
20-30KG		4.5	6.5	7.5	10
30-40KG		10	11	12	13.5
40-50KG		13.5	16	20	27.5
50-60KG		32	34	35	38

Edited 6 Years Ago by prodstar: n/a

So this is something you probably want to read in before you get to that stage of the program. Will the file always be in that format?

If so, make a 2D array for it, use a nested for loop and read each element in:

for(i over rows)
     for(j over cols)
           filein >> my2Darray[i][j];

If the file size isn't going to be known at run time we can make some adjustments. Now, when you get a package in that section of your program, use your two data points to determine the correct row and column of your table.

Edited 6 Years Ago by jonsca: n/a

So this is something you probably want to read in before you get to that stage of the program. Will the file always be in that format?

If so, make a 2D array for it, use a nested for loop and read each element in:

for(i over rows)
     for(j over cols)
           filein >> my2Darray[i][j];

If the file size isn't going to be known at run time we can make some adjustments. Now, when you get a package in that section of your program, use your two data points to determine the correct row and column of your table.

yes i need to make a 2D array but i don't know how to, i need the table from the file to be displayed in my program. I did something like this but it came up with hundreds of random numbers.


this is my code

what I declared

const int ARRAY_SIZE = 24;
    float numbers[6][4];
    int COLUMNS;
    int ROWS;
    ifstream inputFile;

this is my code

inputFile.open("shippingcharges.txt");
                                         for (int ROWS = 0; ROWS < 6; ROWS++)
                                         for (int COLUMNS = 0; COLUMNS < 4; COLUMNS++)
                                             cout << numbers[ROWS][COLUMNS] << endl;
                                             
                                         inputFile.close();

You never took any input from the file.

replace line 4 (in the second snippet) with inputFile >> numbers[ROWS][COLUMNS];

Edited 6 Years Ago by jonsca: n/a

You never took any input from the file.

replace line 4 (in the second snippet) with inputFile >> numbers[ROWS][COLUMNS];

yeah i replaced it but i dont see my rows and columns from my txt =/

Did you check if the file is opening properly (you had it in the other version but it's not in this one). I would say if it is opening properly make a little program to test reading in the file by itself.

You never took any input from the file.

replace line 4 (in the second snippet) with inputFile >> numbers[ROWS][COLUMNS];

alright ive fixed it, i've changed some things around but yeah

i get to the point and i end up crashing the program

please check what i did wrong =/

here is my whole code

// This program will calculate the charges for shipping
#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;
int main()
{ // curly #1
    char choice; //choices available in the main menu.
    float weight; // Weight to be entered.
    int weightcategory;
    int shippingzone; // Shipping Zone to be selected.
    int customeridno; // Customer ID Number.
    int zone;
    int charge;
    
    
    int ShippingData[6][4];
    int COLUMNS;
    int ROWS;
    ifstream inputFile;
      
    cout << "Welcome to the DOWHILE FREIGHT ordering cart" << endl;
    cout << "-------------MENU--------------" << endl;
    cout << "1. To begin ordering enter '1'"  << endl;
    cout << "2. To quit enter '2'" << endl;
    cout << "-------------------------------" << endl;
    cout << "Choose an option (enter number):" << endl;
    cin >> choice;
    
    if(choice == '1')
    {   // curly #2      
              customeridno = rand() * rand();
              cout << "----------------------------------------" << endl;
              cout << "Your Customer ID number is " << customeridno << endl;
              cout << "----------------------------------------" << endl;
              cout << "Please Enter the weight of your item" << endl; // Enter the weight of your item
              cout << "We ship items between 1 to 60KG" << endl;
              cin >> weight;
              
              do 
              { // curly #3
              if(weight >= 1 && weight <= 60)
              { // curly #4
                        if (weight >= 1 && weight < 10)
                        weightcategory = 0;
                        else if (weight >= 10 && weight < 20)
                        weightcategory = 1;
                        else if (weight >= 20 && weight < 30)
                        weightcategory = 2;
                        else if (weight >= 30 && weight < 40)
                        weightcategory = 3;
                        else if (weight >= 40 && weight < 50)
                        weightcategory = 4;
                        else if (weight >= 50 && weight <= 60)
                        weightcategory = 5;
                        
                        cout << "You have entered " <<weight<< "KG" << endl;
                        cout << "We are able to ship that amount of weight" << endl;
                        cout << "-----------------------------------------" << endl;
                        cout << "Please Enter the shipping number zone you would like to use" << endl;
                        cout << "We have 4 Shipping zones." << endl;
                        cout << "1. Standard Shipping. " << endl; 
                        cout << "2. Special Standard Shipping." << endl;
                        cout << "3. Express Shipping." << endl;
                        cout << "4. Express Special Shipping." << endl;
                        cout << "-----------------------------------------" << endl;
                        cout << "Choose an Option (enter number):" << endl; // Select the Shipping Zone
                        cin >> shippingzone;
                        
                        do
                        { // curly #5
                        if (shippingzone >=1 && shippingzone <=4)
                        { // curly #6
                                         cout << "You have Selected Shipping Zone " << shippingzone << endl;
                                         zone = shippingzone - 1;
                                         inputFile.open("shippingcharges.txt",ios::in);
                                         inputFile.precision(2);
                                         inputFile.setf(ios::fixed,ios::showpoint);
                                         
                                         inputFile >> ShippingData[ROWS][COLUMNS];
                                         while(!inputFile.eof())
                                         {
                                                                for(int ROWS = 0; ROWS < 6; ROWS++)
                                                                {
                                                                        for(int COLUMNS = 0; COLUMNS < 4; COLUMNS++)
                                                                        {
                                                                                cout << ShippingData << endl;
                                                                                }
                                                                }
                                         }
                                         
                                         inputFile.close();
                                         
                        
                                        
                                                                                    
                                         
                                         
                        } // curly #6
                        else
                        cout << "ERROR - Please enter one of the shipping zones shown above" << endl;
                        cin >> shippingzone;
                        } while (shippingzone >= 1 && shippingzone <= 4); // curly 5
              } // curly #4
              else
              cout << "ERROR - Please Enter weight between 1 to 60KG" << endl;
              cin >> weight;
              } while (choice == '1'); // curly #3
    } // curly #2
    if (choice == '2')
    {
               exit (1);
               }
    else
    cout << "ERROR - Please choose one of the following options" << endl;
    cin >> choice;
    
       
system ("pause");
return 0;
} // curly #1

Edited 6 Years Ago by prodstar: n/a

Take out the while loop on line 85 and move line 84 to 91 (deleting line 91). Your for loop is collecting the data points you do not need a while.

Does your file have the labels in it or is it just the numbers?

i deleted the while loop on line 85

moved 84 to 91

compiled it and it spammed me with random messages such as " 0x28feb0"

my file only has numbers no labels

Change your array back to float and it works.

which one is it ? i forgot =//// so stressed

ShippingData

alright i changed int ShippingData [6][4]; to float ShippingData [6][4];
compiled it came up with the same 02xfeb08 message

here is my code

// This program will calculate the charges for shipping
#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;
int main()
{ // curly #1
    char choice; //choices available in the main menu.
    float weight; // Weight to be entered.
    int weightcategory;
    int shippingzone; // Shipping Zone to be selected.
    int customeridno; // Customer ID Number.
    int zone;
    int charge;
    
    
    float ShippingData[6][4];
    int COLUMNS;
    int ROWS;
    ifstream inputFile;
      
    cout << "Welcome to the DOWHILE FREIGHT ordering cart" << endl;
    cout << "-------------MENU--------------" << endl;
    cout << "1. To begin ordering enter '1'"  << endl;
    cout << "2. To quit enter '2'" << endl;
    cout << "-------------------------------" << endl;
    cout << "Choose an option (enter number):" << endl;
    cin >> choice;
    
    if(choice == '1')
    {   // curly #2      
              customeridno = rand() * rand();
              cout << "----------------------------------------" << endl;
              cout << "Your Customer ID number is " << customeridno << endl;
              cout << "----------------------------------------" << endl;
              cout << "Please Enter the weight of your item" << endl; // Enter the weight of your item
              cout << "We ship items between 1 to 60KG" << endl;
              cin >> weight;
              
              do 
              { // curly #3
              if(weight >= 1 && weight <= 60)
              { // curly #4
                        if (weight >= 1 && weight < 10)
                        weightcategory = 0;
                        else if (weight >= 10 && weight < 20)
                        weightcategory = 1;
                        else if (weight >= 20 && weight < 30)
                        weightcategory = 2;
                        else if (weight >= 30 && weight < 40)
                        weightcategory = 3;
                        else if (weight >= 40 && weight < 50)
                        weightcategory = 4;
                        else if (weight >= 50 && weight <= 60)
                        weightcategory = 5;
                        
                        cout << "You have entered " <<weight<< "KG" << endl;
                        cout << "We are able to ship that amount of weight" << endl;
                        cout << "-----------------------------------------" << endl;
                        cout << "Please Enter the shipping number zone you would like to use" << endl;
                        cout << "We have 4 Shipping zones." << endl;
                        cout << "1. Standard Shipping. " << endl; 
                        cout << "2. Special Standard Shipping." << endl;
                        cout << "3. Express Shipping." << endl;
                        cout << "4. Express Special Shipping." << endl;
                        cout << "-----------------------------------------" << endl;
                        cout << "Choose an Option (enter number):" << endl; // Select the Shipping Zone
                        cin >> shippingzone;
                        
                        do
                        { // curly #5
                        if (shippingzone >=1 && shippingzone <=4)
                        { // curly #6
                                         cout << "You have Selected Shipping Zone " << shippingzone << endl;
                                         zone = shippingzone - 1;
                                         inputFile.open("shippingcharges.txt",ios::in);
                                         inputFile.precision(2);
                                         inputFile.setf(ios::fixed,ios::showpoint);
                                         {
                                                                for(int ROWS = 0; ROWS < 6; ROWS++)
                                                                {
                                                                        for(int COLUMNS = 0; COLUMNS < 4; COLUMNS++)
                                                                        {
                                                                        inputFile >> ShippingData[ROWS][COLUMNS];
                                                                        cout << ShippingData << endl;
                                                                        }
                                                                }
                                         }
                                         inputFile.close();
                                         
                        
                                        
                                                                                    
                                         
                                         
                        } // curly #6
                        else
                        cout << "ERROR - Please enter one of the shipping zones shown above" << endl;
                        cin >> shippingzone;
                        } while (shippingzone >= 1 && shippingzone <= 4); // curly 5
              } // curly #4
              else
              cout << "ERROR - Please Enter weight between 1 to 60KG" << endl;
              cin >> weight;
              } while (choice == '1'); // curly #3
    } // curly #2
    if (choice == '2')
    {
               exit (1);
               }
    else
    cout << "ERROR - Please choose one of the following options" << endl;
    cin >> choice;
    
       
system ("pause");
return 0;
} // curly #1

You are trying to output shippingData on line 87 which will give you the array pointer when you output it. You need to output shippingData[ROWS][COLUMNS] . Output a space after each one so you can see the values separated out (and if you want put a newline outside of the inner loop at the bottom to have it put each row on a new line).

Step back and take it slow. If you're rushing through it you're not thinking it through...

Edited 6 Years Ago by jonsca: n/a

You are trying to output shippingData on line 87 which will give you the array pointer when you output it. You need to output shippingData[ROWS][COLUMNS] . Output a space after each one so you can see the values separated out (and if you want put a newline outside of the inner loop at the bottom to have it put the row on a new line).

Step back and take it slow. If you're rushing through it you're not thinking it through...

actually ive done it, its outputted the data

the thing is its not in a table

Edited 6 Years Ago by prodstar: n/a

Yes.

i editted my post sorry

yeah i managed to output all the data

the thing is its not in a table, more like in a straight line

If you want it in a table put a newline after the inner loop but before the close of the outer loop. It just happens to display that way, it's all in the array in rows/columns.

If you want it in a table put a newline after the inner loop but before the close of the outer loop. It just happens to display that way, it's all in the array in rows/columns.

i have no idea what your saying =/

sorry what new line?

for(int ROWS = 0; ROWS < 6; ROWS++)
{
        for(int COLUMNS = 0; COLUMNS < 4; COLUMNS++)
       {
            inputFile >> ShippingData[ROWS][COLUMNS];
            cout << ShippingData[ROWS][COLUMNS]<<" ";
          //this loop is going for each column in a particular row
         //after each element there is a space
        }
    //we are here after a row has been completed (after we've gone 
    //through all the columns
   cout <<endl;  //or cout <<"\n";
    //so at the end of the row we place a newline character  

}

Edited 6 Years Ago by jonsca: n/a

for(int ROWS = 0; ROWS < 6; ROWS++)
{
        for(int COLUMNS = 0; COLUMNS < 4; COLUMNS++)
       {
            inputFile >> ShippingData[ROWS][COLUMNS];
            cout << ShippingData[ROWS][COLUMNS]<<" ";
          //this loop is going for each column in a particular row
         //after each element there is a space
        }
    //we are here after a row has been completed (after we've gone 
    //through all the columns
   cout <<endl;  //or cout <<"\n";
    //so at the end of the row we place a newline character  

}

hey thanks your help, really apprectice it!!!

one last problem, when the table opens up, its missing the last 2 numbers on the first and second rows 5 & 6.50
dont know why tho =/

For some reason if you put two spaces after each element instead of one then all the numbers come out. I can't figure that one out... but that means they are all in the array at any rate.

There are some other problems like putting braces after your else statements to make sure that it includes,e.g. lines 100 and 101 in your last full code listing above.

This question has already been answered. Start a new discussion instead.