input file error

So this is my homework. However I am having trouble getting the file to open and read me the values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	ifstream inFile;

	int num1, num2, num3;

	inFile.open("animaldossier.txt");
	
	if (!inFile)
		cout << "Unsuccessful!" << endl;
	
	inFile >> num1;
	inFile >> num2;
	inFile >> num3;

	inFile.close();


	cin.get();
	return 0;
}


The code is incomplete it need like 9 num variables but I cant get the current ones to display.

When I added "C:\\Users\\Jazmin\\Documents\\Fall2013\\CIS-17C\\animaldossier.txt" it wont even tell me its unsuccessful. Animaldossier.txt contains just some animal information. How would I fix my file to open and also read the information correctly. Any help would be much appreciated.
____________________________________________________________________

Bear
Qty: 3
Consumption: 100

Lion
Qty: 6
Consumption: 90

Zebra
Qty: 10
Consumption: 150

Penguin
Qty: 6
Consumption: 12

Python
Qty: 3
Consumption: 6

Elephant
Qty: 3
Consumption: 1600

Eagle
Qty: 2
Consumption: 2

Stingray
Qty: 20
Consumption: 10

Seaturtle
Qty: 3
Consumption: 10
closed account (Dy7SLyTq)
its because you need to print the variables... idk how u expect them to be able to show up if you dont printf, cout, whatever them...
-______- how could I forget...
I added it in and I get some crazy numbers.
Since the file contains text could that mess with my variables?
Probably yes.

The >> operator expects you to know which data type to use for a specific access to the file. That means if you attempt to read an integer it expects to parse digits.

But the text in front of the numbers messes with the parser, so the return value is 0.

There is not really a simple way to separate the number from the text, at least not by a method of the ifstream class. (Not that I know of)

You have to read the whole line and extract the number manually.
Have a look at the string class that might help with parsing the string and functions like strtok() and stoi().
closed account (Dy7SLyTq)
Hes right in that ifstream can't do it I think. However it would be easier imo to do a stringstream because then you have more control of whats in the buffer and what ur grabbing
Topic archived. No new replies allowed.