help needed asap thanks

I want to display odd no then even no then odd no then even no then even no which are entered by person.But first contents of odd are displayed then even displayed.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  #include<iostream>
#include<fstream>
using namespace std;
int main()
{
    string o;
    string e;
    ofstream odd;
    ofstream even;
    odd.open("hi.txt");
    for(int i=0;i<2;i++)
    {
        cout<<"odd number";cin>>o;
        odd<<o;
    }
    odd.close();
        even.open("hello.txt");
    for(int i=0;i<3;i++)
    {
        cout<<"even number";cin>>e;
        even<<e;
    }
    even.close();
    ifstream odd1;
    ifstream even1;
    even1.open("hello.txt");
    odd1.open("hi.txt");
    if((odd1>>o)&&(even1>>e))
    {
        cout<<o<<endl;
        cout<<e<<endl;
    }
    else if(odd1>>o)
        cout<<o<<endl;
    else if(even1>>e)
        cout<<e<<endl;
    even1.close();
    odd1.close();
}
Last edited on
In lines 12 and 18 you iterate a different number of times. Are you just trying to read the first two values of the file for odd numbers and the first three for even?

Also I do not see where you are checking to see if a number if even or odd. You can do this by dividing the number and if there is a remainder then it is an odd number.
Hello suyashsing234,

Here is an idea of what might work better for your input:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	//std::string o;  // <--- Not needed. Would need more code to change from string to int.
	//std::string e;  // <--- Not needed. Would need more code to change from string to int.
	int num{ 0 };  // <--- Better use for input.
	std::ofstream odd;
	std::ofstream even;

	std::string oFileName1{ "Odd.txt" };
	
	std::ofstream outFileOdd;

	outFileOdd.open(oFileName1, std::ios::trunc | std::ios::ate);

	if (outFileOdd.is_open())
	{
		std::cout << "\n File " << oFileName1 << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << oFileName1 << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}

        std::string oFileName2{ "Even.txt" };

	std::ofstream outFileEven;

	outFileEven.open(oFileName2, std::ios::trunc | std::ios::ate);

	if (outFileEven.is_open())
	{
		std::cout << "\n File " << oFileName2 << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << oFileName2 << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}

	while (true)  // <--- Endless loop.
	{
		std::cout << "\n Enter a number. 0 to quit: ";
		std::cin >> num;

		if (num == 0) break;  // <--- To leave while loop when finished.

		if (num % 2 == 0)  // <--- Determines which file to print to.
			outFileEven << num << ' ';
		else
			outFileOdd << num << ' ';
	}

	outFileEven << std::endl;
	outFileOdd << std::endl;

	outFileEven.close();
	outFileOdd.close();


Notice the comments I put in the code. The way I check if the file is open may not be the best way, but it it good for learning.

Lines 11 and 29 I use when first testing the program until it is working then I remove the , std::ios::trunc | std::ios::ate part.

Lines 15, 16 and 33, 34 I comment out when I know the files are open.

From this you should get the idea of how to change the second half or your program.

Hope that helps,

Andy
Last edited on
thanks
i like that if else loop
Topic archived. No new replies allowed.