Hello so I finally was able to read intergers from a file and put them in an array.
Now I have to put those numbers in different arrays. If they are even they go to the even array if odd go to the odd array. Now I thought this was the way to do it but it aint When I cout to see it it gives me a place in memory. Heres my code thank you:

#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
    ifstream input;
    int even [51];
    int odd [51];
    int a[50];
input.open("C:\\Users\\Desktop\\input.txt");
if(input.fail())
    cout<<"FAIL"<<endl;
 while(!input.eof())
 {
     for (int i=0; i<50; i++)
    {input>>a[i];  //contains the intergers from the file
     if (i%2==0)
         even[1];
     else 
         odd[i];

 }
}




 system ("pause");
   return 0; 

}

Recommended Answers

All 16 Replies

The statements at lines 21 and 23 don't do anything. You need to assign the value in a[i] to one or the other of the two target arrays.

Also, don't test i for odd or even, test the value at a[i].

Lastly, you can't use i as the index into the odd or even arrays, you need to keep separate index values for those which you increment when a value gets stored to the arrays.

<grammar Nazi hat on> "intergers" is correctly spelled "integers". </grammar Nazi hat on>

<grammar Nazi hat on> "intergers" is correctly spelled "integers". </grammar Nazi hat on>

<über pedantic mode>

You need to wear your spelling Nazi hat because using the correct word in the correct context with incorrect spelling is not a grammatical error, it's a spelling error.

</über pedantic mode>

:D

Hey thnx for taking the time I see what you are saying however the for loop that I made keeps giving me trouble. So I took it off and I still was able to get values into the a array. This is my new code but when I cout it tells me the array is uninitialized. and yes thank you for that lets not start a grammar genocide

#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
    ifstream input;

input.open("C:\\Users\\Desktop\\input.txt");
if(input.fail())
    cout<<"FAIL"<<endl;
 while(!input.eof())
 {
     int even [51];
    int odd [51];
    int a[50];
    int i=0;
    int x=0;
    int a=0;
    input>>a[i];i++;
    if (i%2==0)
    {x==i;
        even [x];x++;
    }
    else if (i%2!=0)
    {a==i; 
    odd[a];a++;


    }

}


 cout<<odd[3]<<endl;

 system ("pause");
   return 0; 

}

lol as I just woke up

This is my new code but when I cout it tells me the array is uninitialized.

Yes, that's correct. It's harder to see because your indentation is atrociously inconsistent, but the effect is like this:

{
    int odd[15];
}

cout<< odd[3] <<endl;

odd is declared within the braces, and thus its visibility is limited to code within the braces. Therefire, the cout statement doesn't recognize that odd exists. You probably want to declare both odd and even outside of the loop so that they're not reset after each iteration.

deceptikon I see what you are saying I moved it so it would cout in the same brackets but I get nothing just blank. But I get not syntax errors this time

#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
    ifstream input;
  int even [51];
    int odd [51];
    int a[50];
    int i=0;
    int x=0;
    int b=0;
input.open("C:\\Users\\Desktop\\input.txt");
if(input.fail())
    cout<<"FAIL"<<endl;
 while(!input.eof())
 {
     int even [51];
    int odd [51];
    int a[50];
    int i=0;
    int x=0;
    int b=0;
    input>>a[i];i++;
    if (i%2==0)
    {x==i;
        even [x];x++;
    cout<<even[45]<<endl;
    }
     if (i%2!=0)
    odd[b];b++;


}




 system ("pause");
   return 0; 

}

One step forward, two steps back. You're still redefining the arrays inside the loop. Now it's just new variables hiding the ones defined outside the loop. You also always print even[45], which to me is nonsensical.

Compare and contrast:

#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    ifstream in("input.txt");

    if (in) {
        int even[50], odd[50];
        int m = 0, n = 0;
        int value;

        // Read up to 50 numbers from the file
        while (in>> value && (m < 50 && n < 50)) {
            if (value % 2 == 0 && m < 50)
                even[m++] = value;
            else if (n < 50)
                odd[n++] = value;
        }

        // Display the results
        cout<<"Even\tOdd\n";

        for (int i = 0; i < m && i < n; ++i) {
            if (i < m)
                cout<< even[i];

            cout.put('\t');

            if (i < n)
                cout<< odd[i];

            cout.put('\n');
        }
    }
}

Gee man thnx! lol No way was I going to be able to do that, so one quick thing in line 18 and 20, is that where the even array and odd get their content? Sorry its because I need to find the highest and lowest number in each as well as the avg and numbers higher than the avg so I wanna know Where the contents are so I can begin the math . Thank you brony lol

Yes, what deceptikon gave in in lines 18 & 20 fill th odd/even arrays. When done filling those arrays, m and n give you the count of items in each of them. With that, you can examine the arrays to find the various values you need.

deceptickon, my Grammar Nazi hat is an all emcompassing model, good for all that is written poorly on the interwebz.

good for all that is written poorly on the interwebz.

That's a guaranteed ulcer. ;)

ahh ok so somthing like this?

if (value % 2 == 0 && m < 50)
                even[m++] = value;
            int max=0;
            for (int i=0;i<value;i++)
            {if ( max>m++ )
            max=m++;i++;

            }

           if (n < 50)
                odd[n++] = value;

m is the count of how many items in even[]. Don't change m after the array has been filled.

line 3: max should not be set to 0, what if all the values read in are negative? Set max to be the first element of the array.

line 4: uses m as the limit.

line 5: compare max to the element of the array at index i

line 6: if array element is larger than max, set max to that element's value. Don't mess with i inside the loop!

Ok I see ypour point

if (value % 2 == 0 && m < 50)
  even[m++] = value;
int max =even[0];
for (int i=0;i<m;i++)
{if (max >even[i])
    max=even[i];}

Sorry didnt mean to post twice! but thats what I did I dont know if thats correct tho because when I try to cout the max it gives me a bunch of numbers maybe because I try to cout in a loop or something

Check the direction of your comparison in line 5

ohh right thank you for pointing it out heres the update, what do you think about the for loop is that correct?

    int max =even[0];
    for (int i=0;i<m;i++)
    {if (even[i]>max)
        max=even[i];}

That looks correct for finding max of that array. You will use similar methods to fin minimums, and do a bit different work in the loops to find averages and such.

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.