|
slacker008
Resident Psychiatrist


Registered: 05/21/08
Posts: 1,103
Loc: A wave of reality.
|
Anyone know C/C++ programming?
#9172676 - 11/02/08 02:16 PM (15 years, 3 months ago) |
|
|
Im stumped. I have an assignment to find prime numbers between 1 and n. Im getting parse errors and Im probably missing something very very simple. Want to help? 
#include <iostream>
int main() { int num , prime;
do { cout << "Give me an number from 1 to 100! "; cin >> num; if (num < 1 || num > 100) cout << "Please follow the instructions!" << endl << endl; } while (num > 100 || num < 1);
int a = 2, b = 2;
cout << endl << "The primes of that number are: " ; for ( a <= num ) { prime = 1; for ( b <= a ) a++ { if ( a == b ) else if( a % b == 0 ) prime = 0 b++ } if (prime) cout << " " << a ; } return 0; }
My errors are: 0011 do { 0012 cout << "Give me an number from 1 to 100! "; 0013 cin >> num; 0014 if (num < 1 || num > 100) 0015 cout << "Please follow the instructions!" << endl << endl; } 0016 while (num > 100 || num < 1); 0017 0018 int a = 2, b = 2; 0019 0020 cout << endl << "The primes of that number are: " ; 0021 for ( a <= num ) parse error before `)' 0022 { 0023 0024 prime = 1; 0025 for ( b <= a ) 0026 a++ 0027 { 0028 0029 if ( a == b ) 0030 else if( a % b == 0 ) 0031 prime = 0 0032 b++ 0033 } 0034 if (prime) 0035 cout << " " << a ; 0036 } 0037 return 0; 0038 } 0039 confused by earlier errors, bailing out
-------------------- ..beneath the chaos of the world, all is secretly well.. Rebuilding my DNA.
|
CokedUpHobit64



Registered: 10/01/07
Posts: 2,053
Last seen: 3 years, 10 months
|
Re: Anyone know C/C++ programming? [Re: slacker008]
#9172685 - 11/02/08 02:18 PM (15 years, 3 months ago) |
|
|
God I hated doing that shit.
--------------------
So good to see you, I've missed you so much.
|
slacker008
Resident Psychiatrist


Registered: 05/21/08
Posts: 1,103
Loc: A wave of reality.
|
|
Yeah, and Im -currently- hating it. haha
-------------------- ..beneath the chaos of the world, all is secretly well.. Rebuilding my DNA.
|
koppie
astral projectile


Registered: 07/23/04
Posts: 2,653
Loc: cloud hidden
|
Re: Anyone know C/C++ programming? [Re: slacker008]
#9172739 - 11/02/08 02:31 PM (15 years, 3 months ago) |
|
|
Shouldn't the for line be:
for(;a<=num;)
or just do away with the a++ line and the a=2 and write:
for(a=2;a<=num;a++)
If you keep a++, follow it with a ; ditto with tha b++ line. In c all expressions need to end in a semicolon. They're not separators like in Pascal.
But I'm better in c than c++ so I can be wrong.
Edited by koppie (11/02/08 02:33 PM)
|
slacker008
Resident Psychiatrist


Registered: 05/21/08
Posts: 1,103
Loc: A wave of reality.
|
Re: Anyone know C/C++ programming? [Re: koppie]
#9172764 - 11/02/08 02:36 PM (15 years, 3 months ago) |
|
|
Our class is mainly C so your probably right. Ill try it!
-------------------- ..beneath the chaos of the world, all is secretly well.. Rebuilding my DNA.
|
freddurgan
Techgnostic



Registered: 01/11/04
Posts: 3,648
Last seen: 11 years, 8 months
|
Re: Anyone know C/C++ programming? [Re: slacker008]
#9172865 - 11/02/08 02:59 PM (15 years, 3 months ago) |
|
|
Either in C or C++ you need to have 3 statements in a for loop. The for loop will always have 3 semi-colons, for(initialize; condition; action), the only difference in C/C++ being that you can't declare a variable in the first section in C, so C it would be
int a; for(a = 0; a < blah; a++)
and in C++ it would be for(int a = 0; a < blah; a++)
|
slacker008
Resident Psychiatrist


Registered: 05/21/08
Posts: 1,103
Loc: A wave of reality.
|
Re: Anyone know C/C++ programming? [Re: freddurgan]
#9173082 - 11/02/08 03:46 PM (15 years, 3 months ago) |
|
|
I got it running but it skips 1 as a prime. 0 outputs nothing.
-------------------- ..beneath the chaos of the world, all is secretly well.. Rebuilding my DNA.
|
Legend9123



Registered: 09/24/06
Posts: 2,590
Last seen: 9 months, 7 days
|
Re: Anyone know C/C++ programming? [Re: slacker008]
#9173365 - 11/02/08 04:49 PM (15 years, 3 months ago) |
|
|
1 isn't a prime nor is 0. A prime number has EXACTLY two factors, 1 and itself.
-------------------- Those who would give up a little freedom to get a little security shall soon have neither. -Benjamin Franklin
|
DieCommie

Registered: 12/11/03
Posts: 29,258
|
Re: Anyone know C/C++ programming? [Re: Legend9123]
#9173374 - 11/02/08 04:51 PM (15 years, 3 months ago) |
|
|
|
|