Hey,

So I am trying to get several numbers (below ) to be stored in various variables. Could you look at my code and tell me what I am messing up? (trying to emulate an example from class that I missed, stupid swine flu...))

the numbers (yes there is a blank on line 6)

19
367
804
392
375

28903
98868
38
9287
9835

my code

# include <iostream> 
# include <fstream> 
# include <cstdlib>
# include <string>

using namespace std;
 
int main () 
{


	ifstream in_stream;
	ofstream out_stream;

	 in_stream.open("lab6file.txt");
	 out_stream.open("outfile.txt");

	int first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth;	
	in_stream >> first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth;
	
	cout << first << endl;



	in_stream.close();
	out_stream.close();



return(0);

}

the problem right now is that when i out "first" it gives me a random number that is not in the list.

thanks for the feedback.

in_stream >> first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth;

Each variable should be separated by the extraction operator, not commas:

in_stream >> first >> second >> third >> fourth >> fifth
          >> sixth >> seventh >> eighth >> ninth >> tenth;

ahhh the flaws of copy and paste... haha

so i changed that but "first" is still outputting a random number...

The file is probably not open. Initialize first to something like 0 and see if the program prints that value instead of garbage. If it does, the file failed to open. This is a condition you can test easily without playing games with the variables:

ifstream in_stream("lab6file.txt");

if (!in_stream)
{
    cerr << "file not open\n";
}

ok so i had the text file named incorrectly, thanks for catching that.

here is where i am now.

# include <iostream> 
# include <fstream> 
# include <cstdlib>
# include <string>

using namespace std;
 
int main () 
{


	ifstream in_stream;
	ofstream out_stream;

	 in_stream.open("lab_6_input.txt");
	 out_stream.open("outfile.txt");

	 #
if (!in_stream)
#
{
#
cerr << "file not open\n";
#
}

	int first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth;	
	in_stream >> first >> second >> third >> fourth >> fifth >> sixth >> seventh >> eighth >> ninth >> tenth;
	
	cout << first << endl;

	if first % 2 == 0;
	{
      cout << first << " is even." << endl;
	}	
else if 
	{
		cout << first << " is odd." << endl;

	}


		          

	in_stream.close();
	out_stream.close();









	







	
	return(0);

}

The numbers are now associated with the variables.

Now I am trying to check if they are even or odd.

Im assuming that

first % 2 == 0;

it would be an even number otherwise it would be odd. for some reason though when I try the if else its giving me a syntax error on the identifier "first"

error C2061: syntax error : identifier 'first'

any ideas?

if first % 2 == 0;

Your syntax is close. Do not forget the parentheses, and lose the semicolon:

if (first % 2 == 0)

Ok so the following works, but i have to ask if there is a better/more efficient way of doing this. also the professor suggested we use a count controlled while loop to read the values from the text doc. I dont see how this could work though. Would i be able to say (dont know what the syntax would be) "each time i find a number in the text document add one to timer, run while timer < 10 " or something?

i have the rest of it down i think, but again it seems really inefficient to me, what if there were like 1000 numbers?

# include <iostream> 
# include <fstream> 
# include <cstdlib>
# include <string>

using namespace std;
 
int main () 
{


	ifstream in_stream;
	ofstream out_stream;

	 in_stream.open("lab_6_input.txt");
	 out_stream.open("outfile.txt");

	 #
if (!in_stream)
#
{
#
cerr << "file not open\n";
#
}

	int first, second, third, fourth, fifth, sixth, seventh, eighth, ninth, tenth;	
	in_stream >> first >> second >> third >> fourth >> fifth >> sixth >> seventh >> eighth >> ninth >> tenth;
	
	

	if (first % 2 == 0)
	{
      cout << first << " is even." << endl;
	}	
	else
	{
		cout << first << " is odd." << endl;

	}
	
	if (second % 2 == 0)
	{
      cout << second << " is even." << endl;
	}	
	else
	{
		cout << second << " is odd." << endl;

	}

	if (third % 2 == 0)
	{
      cout << third << " is even." << endl;
	}	
	else
	{
		cout << third << " is odd." << endl;

	}

	if (fourth % 2 == 0)
	{
      cout << fourth << " is even." << endl;
	}	
	else
	{
		cout << fourth << " is odd." << endl;

	}

	if (fifth % 2 == 0)
	{
      cout << fifth << " is even." << endl;
	}	
	else
	{
		cout << fifth << " is odd." << endl;

	}
		          
	if (sixth % 2 == 0)
	{
      cout << sixth << " is even." << endl;
	}	
	else
	{
		cout << sixth << " is odd." << endl;

	}
	
	if (seventh % 2 == 0)
	{
      cout << seventh << " is even." << endl;
	}	
	else
	{
		cout << seventh << " is odd." << endl;

	}

	if (eighth % 2 == 0)
	{
      cout << eighth << " is even." << endl;
	}	
	else
	{
		cout << eighth << " is odd." << endl;

	}

	if (ninth % 2 == 0)
	{
      cout << ninth << " is even." << endl;
	}	
	else
	{
		cout << ninth << " is odd." << endl;

	}

	if (tenth % 2 == 0)
	{
      cout << tenth << " is even." << endl;
	}	
	else
	{
		cout << tenth << " is odd." << endl;

	}


	in_stream.close();
	out_stream.close();









	







	
	return(0);

}

HA, figured it out

Thanks for the help everyone

# include <iostream> 
# include <fstream> 
# include <cstdlib>
# include <string>

using namespace std;
 
int main () 
{


	ifstream in_stream;
	ofstream out_stream;

	 in_stream.open("lab_6_input.txt");
	 out_stream.open("outfile.txt");

	 // Determine if file is open
	
	if (!in_stream)
	{
	
		cerr << "file not open\n";
	}

	
	// Determination of odd or even
	
	int number;
	
		int n(0);

		do
		{
			in_stream >> number;

			if (number % 2 == 0)
			{
				cout << number << " is even." << endl;
			}	
			else
			{
			cout << number << " is odd." << endl;

			}
	
	
		n = n++;
		
		}

		

		while (n < 10);
	
	


	in_stream.close();
	out_stream.close();


	
	return(0);

}
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.