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]
#include <iostream>
#include <fstream>
#include <cassert> // assert()
#define NUM_VALUES 50 // Easier to maintain/edit
usingnamespace 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(unsignedint 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(unsignedint 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.
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.
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.
#include <iostream>
#include <fstream>
// return true if n is a prime number; false otherwise
bool is_prime( int n )
{
if( n < 2 ) returnfalse ; // less than two: not prime
if( n == 2 ) returntrue ; // two: prime
if( n%2 == 0 ) returnfalse ; // 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
returntrue ; // if n comes through the division tests
}
int main()
{
constchar* input_file_name = "numbersinput.txt" ;
constchar* 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
}
}
}