CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2008
    Posts
    29

    Having trouble with program that reads and prints arrays

    Im trying to write a program that reads in and prints out arrays. The file that the program reads is a txt file which just looks like
    Code:
    Nixon 200
    Ford 300
    Kennedy 250
    Johnson 453
    Dole 319
    I have written a program that works well when there arent any words and just prints the numbers, but when i try to do one which reads and prints this out the program compiles but nothing happens. Here is my code:
    Code:
    #include <iostream> 
    #include <fstream> 
    using namespace std; 
    bool trytoread(ifstream *ps,int *pn) 
    { 
      *ps>>*pn; //ps is a pointer... so is pn 
      if (!*ps) 
        return false; 
      return true; 
    } 
    int main ()  
    { 
      int num; 
         char fname[80];  
      ifstream inStream; cout<<"Enter filename: "; 
      cin>>fname; 
    inStream.open(fname);  
      if (!inStream) 
      { 
        cout<<"bad open"; 
        exit(1); 
      } 
      while(trytoread(&inStream,&num)) 
        cout<<num<<endl; 
        system("PAUSE");
        return 0; 
    
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with program that reads and prints arrays

    You're trying to read an int when the data in the file is the word "Nixon". What do you expect to happen in this case?

  3. #3
    Join Date
    Apr 2008
    Posts
    15

    Re: Having trouble with program that reads and prints arrays

    By just taking a quick glance at your code, it seems like all you want to do is print what's in the file to the screen. Is that what you want to do?

    Also, you are reading in an int, not a string. To print a name you would need to declare a string, then read in the name into the string variable, and then print the variable.

    The problem is that you have a function that works only with integers and not strings. Do you need to use the trytoread function? If not then I would not use it.

    Instead, try this:
    Code:
    #include <iostream> 
    #include <fstream> 
    
    using namespace std;
    
    int main ()
    {
         int num=0;
         string name;
         char fname[80];  
         ifstream inStream;
    
         cout<<"Enter filename: " << endl; 
         cin>>fname;
    
         inStream.open(fname);
    
         if (!inStream) 
         { 
             cout<<"bad open"; 
             return 0; 
          }
    
         while(!inStream.eof())
          {
                inStream >> name;
                inStream >> num;
                cout << name << " " << num << endl;
           }
    
         return 0;
    }
    I hope this helps you.

  4. #4
    Join Date
    Feb 2008
    Posts
    29

    Re: Having trouble with program that reads and prints arrays

    oh ok thank you that helped a lot. Now lets say i wanted to read through the text file and print out the one with the maximum amount of votes, how would i do that? I want to be able to do this in two functions as well, so should i create a void function like this?
    Code:
    void maxVotes(num)
    {
    int x=0;
    int i;
    for(i=0;i<num;i++)
    if votes[i]>votes[x]
    x=i;
    return x;
    }

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with program that reads and prints arrays

    That looks just about right, except that normally you'd pass votes into the function as well as num.

  6. #6
    Join Date
    Apr 2008
    Posts
    26

    Re: Having trouble with program that reads and prints arrays

    Quote Originally Posted by hrstar24
    oh ok thank you that helped a lot. Now lets say i wanted to read through the text file and print out the one with the maximum amount of votes, how would i do that? I want to be able to do this in two functions as well, so should i create a void function like this?
    Code:
    void maxVotes(num)
    {
    int x=0;
    int i;
    for(i=0;i<num;i++)
    if votes[i]>votes[x]
    x=i;
    return x;
    }

    I think you can achieve this in the same function as read.

    Initialize two more variables
    Code:
    int maxvotes=0;
    string max;
    while(!inStream.eof())
          {
                inStream >> name;
                inStream >> num;
                if (num>maxvotes)
                {
                       maxvotes = num;
                       max=name;
                 }
                cout << name << " " << num << "Max so far " << max << " with "<<maxvotes<<endl;
           }

  7. #7
    Join Date
    Feb 2008
    Posts
    29

    Re: Having trouble with program that reads and prints arrays

    thanks that helped a lot, but for my program i need to have the max in a seperate function. Im trying to adapt the code, but im having trouble defining the max function and calling the max function in main. How should i go about doing that? The max function should be a void function right?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Having trouble with program that reads and prints arrays

    Not if you expect to return anything (such as a max index)....

  9. #9
    Join Date
    Apr 2008
    Posts
    26

    Re: Having trouble with program that reads and prints arrays

    Quote Originally Posted by Lindley
    Not if you expect to return anything (such as a max index)....
    I agree but what about the case where two distinct entries have the same number of votes which is equal to max votes.

    If its only about printing values then it can be void function. If you need to do something with the data I would say return an int [] which has all the indices with the max value. You can also pass an integer pointer *num_max to the function to set as to how many values have the max votes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured