write neg and pos numbers

im supposed to create a program that reads in a list of integers from the terminal and writes the negative numbers to one file and the positive numbers to another file.

i got most of it doen but for some reason its not writting the negative numbers. any ideas on what im doing wrong?


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <fstream>
#include <string>
using namespace std;



int main()
{
    int pos_num = 0;
    int neg_num = 0;
    int positive_numbers = pos_num % 5;
    int negative_numbers = neg_num % 5;
    int input_numbers;
   
    string name1;
    string name2;
    ofstream fout1;
    ofstream fout2;
    
    

    cout << "Enter file name for positive integers: ";
    cin >> name1;
    
    cout << "Enter file name for negative integers: ";
    cin >> name2;
    
   
    
    fout1.open(name1.c_str(), ios::out);
    
    if (!fout1.is_open())
    {
        cerr << "Unable to open file " << name1 << endl;
    }
    
    fout2.open(name2.c_str(), ios::out);
    
    if (!fout2.is_open())
    {
        cerr << "Unable to open file " << name2 << endl;
    }
    
    
    
    cout << "Enter list of negative and positive integers (followed by 0): " << endl;
    
    while ((cin >> input_numbers) && (input_numbers != 0))
    {
        if ((input_numbers > 0) && (positive_numbers !=0))
        {
            fout1 << input_numbers << " ";
            pos_num++;
        }
        else if ((input_numbers > 0) && (positive_numbers == 0))
        {
            fout1 << input_numbers << endl;
            pos_num++;
        }
        else if ((input_numbers < 0) && (negative_numbers !=0))
        {
            fout2 << input_numbers << " ";
            neg_num++;
        }
        else if ((input_numbers < 0) && (negative_numbers == 0))
        {
            fout2 << input_numbers << endl;
            neg_num++;
        }
    }
    
    
    
    fout1 << endl;
    fout2 << endl;
    
    cout << name1 << " contains " << pos_num << " positive integers." << endl;
    cout << name2 << " contains " << neg_num << " negative integers." << endl;
    
    fout1.close();
    fout2.close();
    
    
    
    return 0;
}
its not writting the negative numbers

Do you mean they are not written to output file? or they are not counted?

Try to describe the problem more thoroughly.

Also try put cout>>"negative">>neg_num to your third and fourth ifs to debug whether you get there or not.
Topic archived. No new replies allowed.