Need help with arrays and file I/O

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dkwan
    New Member
    • Feb 2008
    • 13

    Need help with arrays and file I/O

    I am trying to calculate the average of 4 integers from a text file and then find the maximum and minimum. This is where i have reached...I am having problems to read the integers and calculate the average..
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {   char numbers[100];
        int sum;
        int n,i,firstnum,secondnum,thirdnum,fourthnum,count1,count2;
        int average;
        int min = 0; 
        int max = 0; 
        
        ifstream streamin;
        ofstream streamout;
        if (argc!=3)
           { cerr<< "Usage:Program input_file_output_file\n";
             exit(1);
           }
           
           if(!streamin)
              { cerr <<"Cannot open file!" <<argv[1] <<"for reading" <<endl;
                exit(1);
                }
         if(!streamout)
              { cerr <<"Cannot open file!" <<argv[2] <<"for reading" <<endl;
                exit(1);
                }
         streamin.open(argv[1]);
         
        
        streamin.getline(numbers,100);
        
        
        cout <<"The four integers of the input file are: " << numbers <<endl;
       
       streamout.open(argv[2]);
      
       
       streamin.close();
       streamout.close();
  • hsn
    New Member
    • Sep 2007
    • 237

    #2
    i understand your problem
    first put the if statement of streamin after the line where you say streamin.open,
    and don't use the if statement of streamout until u define which file streamout is going to use.

    your other problem is with getline
    you are entering the number 100 as one of the parameters, which means 100 chars from the file. and you only need to read 4 digits not 100.
    look through it well

    Comment

    • dkwan
      New Member
      • Feb 2008
      • 13

      #3
      I have made some changes..The average part is not working..can you tell me if im doing it right..thanks
      Code:
      #include <cstdlib>
      #include <iostream>
      #include <fstream.h>
      #include <stdlib.h>
      
      using namespace std;
      
      int main(int argc, char *argv[])
      {   char numbers[11];
          int sum=0;
          int i,;
          int average;
          int min = 0; 
          int max = 0; 
          
          ifstream streamin;
          ofstream streamout;
          if (argc!=3)
             { cerr<< "Usage:Program input_file_output_file\n";
               exit(1);
             }
             
            
           
           streamin.open(argv[1]);
            if(!streamin)
                { cerr <<"Cannot open file!" <<argv[1] <<"for reading" <<endl;
                  exit(1);
                  }
          
          streamin.getline(numbers,11);
          cout <<"The four integers of the input file are: " << numbers <<endl;
         
         streamout.open(argv[2]);
         if(!streamout)
                { cerr <<"Cannot open file!" <<argv[2] <<"for reading" <<endl;
                  exit(1);
                  }
        
         for(i=0;i<11;++i)
         {
            sum+=numbers[i];
         }
         
         average=sum/4;
         
         cout<<average;
         
         streamin.close();
         streamout.close();
          
        
             
          
          system("PAUSE");
          return EXIT_SUCCESS;
      }

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        you are reading the input worng
        you are reading it into char value and after that you are adding that to a number that will not work.
        you have to add a number to a number to char to a number.
        try to convert the char into a number
        if you have char c='1';
        int i =c-'0';
        then i==1;
        the same thing for any number , this works only for single digit in a char (advicing to to do it for more than one digit in a char);
        you can solve it

        Comment

        • hsn
          New Member
          • Sep 2007
          • 237

          #5
          listen here is a very easy way to solve you question.
          you don't need to solve this question by using getline()
          you know you have four values
          so you create n1,n2,n3,n4 as int.
          you know when you use
          cin>>s; //where s is a string.
          if the input was the scripts
          s will be "the" without "scripts".
          the operator ">>" will not read after " "(space).
          so use ">>" rather than getline();
          so you wirte 4 times
          streamin>>n1;
          streamin>>n2;
          streamin>>n3;
          streamin>>n4;

          then you will have the four values.
          and then do the average and summation part
          don't forget to use double for average so what ever after . will be their like 2.5 or so.


          HOPE i was helpful

          regards
          hsn

          Comment

          • dkwan
            New Member
            • Feb 2008
            • 13

            #6
            if you have char c='1';
            int i =c-'0';
            then i==1;

            Can you please explain more about this part..

            Comment

            • dkwan
              New Member
              • Feb 2008
              • 13

              #7
              Thanks a lot!!!
              I have been able to solve it..Great!!

              Comment

              • hsn
                New Member
                • Sep 2007
                • 237

                #8
                if you have a char c = '3';
                int n=c-'0';
                n==3;
                it is a rule me my self i don't understand how it is working. but it does work i am sure of it.

                good luck and i hope i helped you

                regards

                hsn

                Comment

                • dkwan
                  New Member
                  • Feb 2008
                  • 13

                  #9
                  oops..i messed up everything..i thought the solution you gave me was working..i am getting confused..help me please..

                  Comment

                  • hsn
                    New Member
                    • Sep 2007
                    • 237

                    #10
                    Originally posted by dkwan
                    oops..i messed up everything..i thought the solution you gave me was working..i am getting confused..help me please..

                    where are u getting confused exactly?????

                    Comment

                    • dkwan
                      New Member
                      • Feb 2008
                      • 13

                      #11
                      Code:
                      #include <cstdlib>
                      #include <iostream>
                      #include <fstream.h>
                      #include <stdlib.h>
                      
                      using namespace std;
                      
                      int main(int argc, char *argv[])
                      {  string numbers;
                          int sum=0;
                          int i,n1,n2,n3,n4;
                          int average;
                          int min = 0; 
                          int max = 0; 
                          
                          ifstream streamin;
                          ofstream streamout;
                          if (argc!=3)
                             { cerr<< "Usage:Program input_file_output_file\n";
                               exit(1);
                             }
                             
                            
                           
                           streamin.open(argv[1]);
                            if(!streamin)
                                { cerr <<"Cannot open file!" <<argv[1] <<"for reading" <<endl;
                                  exit(1);
                                  }
                          streamin>>numbers;
                          
                          
                          cout <<"The four integers of the input file are: " << n1 <<n2 <<n3 <<n4 <<endl;
                         
                         streamout.open(argv[2]);
                         if(!streamout)
                                { cerr <<"Cannot open file!" <<argv[2] <<"for reading" <<endl;
                                  exit(1);
                                  }
                        
                         
                         
                         streamin.close();
                         streamout.close();
                          
                             
                          
                          system("PAUSE");
                          return EXIT_SUCCESS;
                      }
                      is this the way to do it??i am having problems to read the numbers in the text file

                      Comment

                      • hsn
                        New Member
                        • Sep 2007
                        • 237

                        #12
                        as i told you the operator >> will read a value until a space is faced so if the input is "1 2 3 4" and you said streamin>>n1;
                        n1==1;
                        don't use the streamin>>numbe rs;
                        use it like this
                        streamin>>n1
                        streamin>>n2
                        streamin>>n3
                        streamin>>n4

                        by this n1==1,n2==2,n3= =3,n4==4;
                        it is like using cin;

                        Comment

                        • dkwan
                          New Member
                          • Feb 2008
                          • 13

                          #13
                          i tried it but it is not working!! help me plzzz

                          Comment

                          • sicarie
                            Recognized Expert Specialist
                            • Nov 2006
                            • 4677

                            #14
                            Originally posted by dkwan
                            i tried it but it is not working!! help me plzzz
                            Okay, how is it not working? Are you getting an error? Or do the numbers not come out right? Can we see the code (and the errors, if any)?

                            Comment

                            • dkwan
                              New Member
                              • Feb 2008
                              • 13

                              #15
                              I have the integers 8 55 26 99 in the input file but it is being displayed as 8552699.
                              i modified the code as hsn asked me..
                              Code:
                              #include <cstdlib>
                              #include <iostream>
                              #include <fstream.h>
                              #include <stdlib.h>
                              
                              using namespace std;
                              
                              int main(int argc, char *argv[])
                              {  string numbers;
                                  int sum=0;
                                  int i,n1,n2,n3,n4;
                                  int average;
                                  int min = 0; 
                                  int max = 0; 
                                  
                                  ifstream streamin;
                                  ofstream streamout;
                                  if (argc!=3)
                                     { cerr<< "Usage:Program input_file_output_file\n";
                                       exit(1);
                                     }
                                     
                                    
                                   
                                   streamin.open(argv[1]);
                                    if(!streamin)
                                        { cerr <<"Cannot open file!" <<argv[1] <<"for reading" <<endl;
                                          exit(1);
                                          }
                                 streamin>>n1;
                                 streamin>>n2;
                                 streamin>>n3;
                                 streamin>>n4;
                                  
                                  
                                  cout <<"The four integers of the input file are: " ;<< n1 <<n2 <<n3 <<n4 <<endl;
                                 
                                 streamout.open(argv[2]);
                                 if(!streamout)
                                        { cerr <<"Cannot open file!" <<argv[2] <<"for reading" <<endl;
                                          exit(1);
                                          }
                                
                                 
                                 
                                 streamin.close();
                                 streamout.close();
                                       
                                  
                                  system("PAUSE");
                                  return EXIT_SUCCESS;
                              }

                              Comment

                              Working...