error with code

cant figure out how to fix this error. here is the error code i get:
line col
11 9 [Error] statement has no effect [-Werror=unused-value]
11 9 [Error] statement is a reference, not call, to function 'ifPrime' [-Werror=address]
12 6 [Error] unused variable 'value' [-Werror=unused-variable]

help is greatly appreciated.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
 
#include <iostream>
 #include <fstream>
 using namespace std;

void ifPrime(int, int, int);

int main()
 {
 ifstream inFile;
 ifstream savedata;
 ifPrime;
 int value;
	int value1; 
	int value2; 
	int value3; 
	int value4; 
	int value5; 
	int value6;
	int value7; 
	int value8;
	int value9;
	int value10; 
	int value11;
	int value12; 
	int value13; 
	int value14; 
	int value15; 
	int value16; 
	int value17; 
	int value18; 
	int value19; 
	int value20;
    int value21; 
	int value22; 
	int value23;
	int value24; 
	int value25; 
	int value26; 
	int value27; 
	int value28;
	int value29; 
	int value30; 
	int value31; 
	int value32; 
	int value33; 
	int value34; 
	int value35; 
	int value36; 
	int value37; 
	int value38; 
	int value39;
	int value40;
    int value41;
	int value42; 
	int value43; 
	int value44; 
	int value45; 
	int value46; 
	int value47;
	int value48; 
	int value49; 
	int value50;

 inFile.open("numbersinput.txt");
 cout << "reading data from the file" << endl;

 			inFile >> value1; 
            inFile >> value2; 
            inFile >> value3; 
			inFile >> value4; 
            inFile >> value5; 
            inFile >> value6; 
            inFile >> value7; 
            inFile >> value8;
            inFile >> value9;
            inFile >> value10;
            inFile >> value11; 
            inFile >> value12; 
            inFile >> value13; 
            inFile >> value14; 
            inFile >> value15; 
            inFile >> value16; 
            inFile >> value17; 
            inFile >> value18;
            inFile >> value19;
            inFile >> value20;
            inFile >> value21; 
            inFile >> value22; 
            inFile >> value23; 
            inFile >> value24; 
            inFile >> value25; 
            inFile >> value26; 
            inFile >> value27; 
            inFile >> value28;
            inFile >> value29;
            inFile >> value30;
            inFile >> value31; 
            inFile >> value32; 
            inFile >> value33; 
            inFile >> value34; 
			inFile >> value35;
			inFile >> value36;  
            inFile >> value37; 
            inFile >> value38;
            inFile >> value39;
            inFile >> value40;
            inFile >> value41;
            inFile >> value42;
            inFile >> value43;
            inFile >> value44;
            inFile >> value45;
            inFile >> value46;
            inFile >> value47;
            inFile >> value48;
            inFile >> value49;
            inFile >> value50;
            inFile.close();
            
            cout << "now saving numbers";
            
          savedata.open("inputnumbers.txt");
          	savedata >> value1; 
            savedata >> value2; 
            savedata >> value3; 
			savedata >> value4; 
           	savedata >> value5; 
            savedata >> value6; 
            savedata >> value7; 
            savedata >> value8;
            savedata >> value9;
            savedata >> value10;
            savedata >> value11; 
            savedata >> value12; 
            savedata >> value13; 
            savedata >> value14; 
            savedata >> value15; 
            savedata >> value16; 
            savedata >> value17; 
            savedata >> value18;
			savedata >> value19;
            savedata >> value20;
            savedata >> value21; 
            savedata >> value22; 
            savedata >> value23; 
            savedata >> value24; 
            savedata >> value25; 
            savedata >> value26; 
            savedata >> value27; 
            savedata >> value28;
            savedata >> value29;
            savedata >> value30;
            savedata >> value31; 
            savedata >> value32; 
            savedata >> value33; 
            savedata >> value34; 
			savedata >> value35;
			savedata >> value36;  
            savedata >> value37; 
            savedata >> value38;
            savedata >> value39;
            savedata >> value40;
            savedata >> value41;
            savedata >> value42;
            savedata >> value43;
			savedata >> value44;
            savedata >> value45;
            savedata >> value46;
            savedata >> value47;
            savedata >> value48;
            savedata >> value49;
            savedata >> value50;
     		savedata.close();
    ifPrime(value1, value2, value3);
        return 0;
        
}

 void IfPrime(int value1, int value2, int value3)
{
	if (value1 /=1, value1/=value1)
	{
		cout << "this number is prime";
	}
	else 
	{
		cout << "this number isnt prime";
	}
}
here is the numbers for the numbersinput file:

2529
2286
1253
1029
692
117
1984
1524
804
154
1068
626
2037
573
177
242
1676
914
1878
278
1851
2875
28
714
1492
244
297
185
50
425
395
317
345
67
221
33
19
141
40
126
76
35
11
77
36
140
105
20
9
5
Hi Ghostman513,

This program seems like a good chance to use a loop. A for loop has the following syntax:
1
2
3
for( initialValue ; conditional ; update ) {
       // Do stuff as long as the conditional is true here
}


Your conditionals for the ifPrime() function are also off.

Here is a link that will explain these things better than I can:
http://www.cplusplus.com/doc/tutorial/control/

Below is a restructured version of your program, although the ifPrime() function is still not correct:
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
#include <iostream>
#include <fstream>
#include <cassert> // assert()

#define NUM_VALUES 50 // Easier to maintain/edit

using namespace std;

void ifPrime(int, int, int);

int main()
{
	ifstream inFile;
	//ifstream savedata;
	ofstream savedata; // This should be an output file stream, not an input file stream
	//ifPrime; What is this line supposed to do?
	int values[NUM_VALUES]; // Declares NUM_VALUES number of integer variables

	inFile.open("numbersinput.txt");
	assert(inFile); // Verifies file was found

	cout << "Reading data from the file..." << endl;

	for(unsigned int i = 0; i < NUM_VALUES; i++) {
		inFile >> values[i];
	}
	
	inFile.close();

	cout << "Now saving numbers...";

	savedata.open("inputnumbers.txt");
	assert(savedata); // Verifies file was opened

	for(unsigned int i = 0; i < NUM_VALUES; i++) {
		savedata << values[i];
	}

	savedata.close();

	ifPrime(values[0], values[1], values[2]);

	return 0;
}

//void IfPrime(int value1, int value2, int value3)
void ifPrime(int value1, int value2, int value3)
{
	//if( value1 /= 1, value1 /= value1 ) This is not a conditional (the way you intend at least)
	if( (value1 /= 1) == ?? || (value1 /= value1) == ?? ) // Every real number is divisible by one and itself
	{
		cout << "this number is prime";
	} else
	{
		cout << "this number isnt prime";
	}
}


You're on the right track for the ifPrime function. I would use the modulus operator (%) which will return the remainder of division. EX: 15 % 2 = 1, since 15 / 2 has one as a remainder. Using that and a loop, iterate through values less than the value1 and break if value1 % loopCount equals 0. Additionally, why are there 3 values being passed to ifPrime? It only uses one of them.
Last edited on
this is for a project where they want us to test 50 numbers from an input file test them to see if they are prime and then if they are prime to put them in an output file.
In this situation, there is no need to store all the values from the file, not in an array, nor in many separate variables.

Just read a single value from the file, test whether or not it is prime, write it to the output file only if it is prime. Repeat.

Function ifprime should have a return type of bool and take a single parameter, the number being tested. Return true in the case of a prime number, otherwise return false.

bool ifPrime(int);

The errors you posted are on lines 12 and 13 of the code you posted:
1
2
 ifPrime;
 int value;

11 9 [Error] statement has no effect [-Werror=unused-value]
The statement ifPrime; does not call ifPrime, it just returns the function's address, which is then thrown away. So the
statement has no effect
on the program.

11 9 [Error] statement is a reference, not call, to function 'ifPrime' [-Werror=address]
Like I said, that statement doesn't call function ifPrime.

12 6 [Error] unused variable 'value' [-Werror=unused-variable]
At line 13 you declare variable "value", but you never use it in the program.

One other bug: function names in C++ are case sensitive, so the function ifPrime() that you declare at line 6 is NOT the same as the function IfPrime that you call define at line 179

> this is for a project where they want us to test 50 numbers from an input file
> test them to see if they are prime and then if they are prime to put them in an output file.

Chervil +1
>> there is no need to store all the values from the file, not in an array, nor in many separate variables.
>> Just read a single value from the file, test whether or not it is prime,
>> write it to the output file only if it is prime. Repeat.

Something along these lines:
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
#include <iostream>
#include <fstream>

// return true if n is a prime number; false otherwise
bool is_prime( int n )
{
    if( n < 2 ) return false ; // less than two: not prime
    if( n == 2 ) return true ; // two: prime
    if( n%2 == 0 ) return false ; // even number greater than two: not prime

    // TO DO rest of the code for the function
    // check if n is divisible by any number 3, 5, 7, 9 ... less than n 
    //      (less than or equal to square root of n, if we want to get fancy)
    // if it is divisible, it is not a prime; return false 

    return true ; // if n comes through the division tests
}

int main()
{
    const char* input_file_name = "numbersinput.txt" ;
    const char* output_file_name = "output.txt" ;

    std::ifstream input_file(input_file_name) ; // ifstream: open for input
    std::ofstream output_file(output_file_name) ; // ofstream: open for output

    int number ; // number to be read in from the input file
    while( input_file >> number ) // for each number read from the input file
    {
        if( is_prime(number) ) // if it is a prime number
        {
            output_file << number << '\n' ; // write it to the output file
        }
    }
}
Topic archived. No new replies allowed.