You are here:

C++/Home work

Advertisement


Question
My friends and i are trying to do a home,and it should store the table for ASTM standards for rebars in two dimensional array.
This is how the table should look like upon execution.
Index   Size   Weight(lb/ft)   Diameter(in)
0   2   0.167      0.250
1   3   0.376      0.375
2   4   0.668      0.500
3   5   1.043      0.625
4   7   1.502      0.750
5   8   2.044      0.875
6   9   2.670      1.000
7   12   3.400      1.128
8   14   4.303      1.270
But instead,our program only prints the table header but not the data.. here is our program coding..

# include <iostream>
# include <stdlib.h>
# include <string>
# include <fstream>
# include <iomanip>

using namespace std;

const int row=10;
const int column=4;
int main ()
{
  string ASMT[10][4];
  int choice;
  
  
  
  ifstream infile;
  ifstream open;
  
  cout << "This program determines the total weight of rebars used in a given"
       << " scenario. The calculation is based on ASTM <American Service for"
      << " testing and materials> "<< endl
      << " standards" << endl;
   
   cout << "\nProgram Menu:"
       << "\n<1> Print ASMT table"
       << "\n<2> Read the length of rebas used from a file"
       << "\n<3> Exit" << endl;
       
   cout <<"Enter the number of your choice  ";
  cin >> choice;
  cout << endl;
  
  infile.open("ASMT.txt");
   
   if(choice==1){
      
      if(infile.fail()) {
       cout<<"File not found!" << endl;
     
     }   
  }
   
   if(choice==1)
   {
      //int s=6;
      for(int i=0;i<row;i++)
     {
      
      for(int j=0;i<column;i++)
        {
         
         infile >> ASMT[i][j];
         cout << setw(4) << ASMT[i][j] << setw(4) << " ";
        }
         cout << endl;
     }
        
  }
     
  
  
  
  
  
  system("pause");
  return 0;
}

Answer
You simply made a mistake and did not finish modifying the nested for loop.  You must change i to j in the "for part of the loop", like the code below :

for(int j=0;j<column;j++)
{
     
      infile >> ASMT[i][j];
      cout << setw(4) << ASMT[i][j] << setw(4) << " ";
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.