'Else' without a previous 'if"

I am trying to make a program that asks user input of a number, if it is prime the program will save it to a file. If it is not prime, the program will not save to a file and display that it is not a prime number.

Line 44 gives me an error and says there is a else without a previous if statement. But there clearly is so I don't know what I did 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
#include <iostream>
#include <fstream>
 using namespace std;

bool isPrime(int);    //Function prototype

 int main()
 {
     //Local variables
     fstream dataFile;
     int number;

     //Asks for user input
     cout << "Please enter a number to find ";
     cout << "out if it is prime: " << endl;
     cin  >> number; //Stores user input

     if (isPrime(number)) //Calls the isPrime function
         cout << number << " is Prime.\n"; //Returns true value

     else
         cout << number << " is not Prime.\n"; //Returns false value

    return 0;
 }

 bool isPrime(int number)
{
     fstream dataFile;
     bool status;


     if
        (number % 1 && number) //If the number is divisible by 1 and itself
         status == true; //Returns true value

         dataFile.open("PrimeNumbers.txt", ios::out);

         cout << "This data has been saved to a file.\n";

        //Closes the file.
         dataFile.close();

     else
        status = false; //Return false value
         /*Does not save numbers in a file*/


return status;
}
You are missing the brackets that contain the code for the if statement.
1
2
3
4
5
6
7
8
 if  (number % 1 && number)     {               //If the number is divisible by 1 and itself
         status == true; //Returns true value
        .......................
        .......................
  }
     else
        status = false; //Return false value
         /*Does not save numbers in a file*/


Thank you. I fixed it and that solved the problem.

But there is an error with my logic. No matter what number I enter, it says it is not prime.
There are plenty of examples just from a google search. Here's a link to another page on this forum that people posted their code to for prime numbers.

http://www.cplusplus.com/forum/general/1125/
I did not find that useful. :/
1
2
3
if
        (number % 1 && number) //If the number is divisible by 1 and itself
         status == true; //Returns true value 



This if statement is incorrect in several ways:

1) % gives you the remainder after division. So, for example... 17%5 would give you 2 because 17/5 has a remainder of 2. With that in mind... ask yourself what the remainder is after a division by 1? It's always going to be zero, because any number divided by 1 is itself with no remainder. Therefore, this if statement is always going to be false because the first portion of it is always 0.


2) && does not do what you think it does. It does not combine operands for the % operator. Instead, it combines the boolean result on its left and right sides. So the left side is "number % 1" and the right side is "number". If both of those sides are nonzero, then you get 'true'. Otherwise you get 'false.

As already mentioned... "number % 1" is always going to be zero, so && will give you false no matter what. But "number" likely is never going to be zero.


3) The logic here doesn't make sense... because every number is divisible by 1 and itself. So even if this statement worked as you intended, it would think that every number is prime.
You are going to have to change your isPrime function
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
    bool isPrime(int number)
{
     fstream dataFile;
     bool status;
    int i=2;
 
    while(i<=number-1)
    {
        if(number%i==0)
        {
            status=false;
            break;
        }
        i++;
    }
    if(i==number)
    {
        status=true;
        dataFile.open("PrimeNumbers.txt", ios::out);

         cout << "This data has been saved to a file.\n";

        //Closes the file.
         dataFile.close();
    }

return status;
}


The code from http://www.mindfreakerstuff.com/2013/05/program-for-check-whether-number-is-prime-or-not-in-c/ was modified to fit what you want to do. It should work just fine for your program
Thank you. I appreciate your taking the time to help me better understand this.
Last edited on
Topic archived. No new replies allowed.