Ok I just started programming again after about 10 years,
and i was doing the suggested beginner problems.

The first one to create a program that returns the factorial of a number.
I think i got it all down right but compiler is saying
"expected primary-expression before else"

here's the code, I made the places the compiler doesn't like magenta.

#include <iostream>
using namespace std;


int main () {
    cout << "This program gives you the factorial of your number.\n";
    cout << "Please enter a number.  ";
    int num, prime = 2;
    cin >> num;
    if (num == 0 || num == 1) {prime = num;}
    if (num < 0) {
        cout << "This program only handles positive numbers."; 
        system ("pause"); 
        return 0;}
    int max = num, E = 1;
    while ((int) max > 2) {
        max = max / 2;
        E++;
    }
    max = E;
    int A = 0, primes[max];
    while (prime <= max) {
        primes[A] = prime; 
        A++;
        int C = 0;
        while (C == 0) {
            prime = prime+2;
            for (int B = A; B>0; B--) {
                if (B == 0) {
                    C = 1;
                else if (prime % primes[b] == 0)
                    B = 0;
                }
            }
        }
    }
    int D = 1;
    prime = primes[0];
    while (num != prime) {
        if (num % prime == 0) {
            cout << prime << "\n";
            num = num / prime;
        else 
            prime = primes[D]; 
            D++;
        }
    }
    cout << prime << "\n";
    system ("pause");
    return 0;
}

Also the second problem (create a program to give a fibonacci
sequence up to given number) went rather well going to add it in case
anyone has some pointers for me.

#include <iostream>
using namespace std;


int main () {
    cout << "This program lists all the numbers in a fibonacci series \n"; 
    cout << "up to a number of your choice. \n" << "Please enter a number?  ";
    int max;
    cin >> max;
    int fib =1, preA =0, preB =0;
    while (fib <= max) {
        cout << fib << "\n";
        preB = preA;
        preA = fib;
        fib = fib+preB;
    }
    system ("pause"); 
    return 0;
}

Oh and i used system ("pause") because it worked and well cin.get() and such didn't, oh looking back i bet i need to put a variable in the brackets don't I oh well thats what i get for copy/pasting it hehe.

Recommended Answers

All 4 Replies

Hmm something removed all my tabulation which made code soooo much easier to read.
Anyone know how to keep tabs in code blocks for legibility?

Anyone know how to keep tabs in code blocks for legibility?

type the code tags in capital letters.

if (B == 0) {
    C = 1;
else if (prime % primes[b] == 0)
     B = 0;
}

Remove the { and the } or do this

if (B == 0) {
  C = 1;
}
else if (prime % primes[b] == 0){
   B = 0;
}

Hmm something removed all my tabulation which made code soooo much easier to read.
Anyone know how to keep tabs in code blocks for legibility?

Change your TABs to SPACEs. There is probably a setting in your IDE options.

As for system("pause");, see this. Put your cin.get() in a loop and watch for the ENTER (\n).

The format for an IF-ELSE statement is:

if (comparison)
{
    code if true
}
else
{
    code if false
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.