Home | Community | Message Board

Sporeworks
This site includes paid links. Please support our sponsors.


Welcome to the Shroomery Message Board! You are experiencing a small sample of what the site has to offer. Please login or register to post messages and view our exclusive members-only content. You'll gain access to additional forums, file attachments, board customizations, encrypted private messages, and much more!

Jump to first unread post Pages: 1
Invisibleslacker008
Resident Psychiatrist
 User Gallery

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? :smile:

#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.


Extras: Filter Print Post Top
OfflineCokedUpHobit64
Male

Folding@home Statistics
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.



Extras: Filter Print Post Top
Invisibleslacker008
Resident Psychiatrist
 User Gallery

Registered: 05/21/08
Posts: 1,103
Loc: A wave of reality.
Re: Anyone know C/C++ programming? [Re: CokedUpHobit64]
    #9172687 - 11/02/08 02:18 PM (15 years, 3 months ago)

Yeah, and Im -currently- hating it. haha


--------------------
..beneath the chaos of the world, all is secretly well..

Rebuilding my DNA.


Extras: Filter Print Post Top
Invisiblekoppie
astral projectile
Male

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)


Extras: Filter Print Post Top
Invisibleslacker008
Resident Psychiatrist
 User Gallery

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.


Extras: Filter Print Post Top
Offlinefreddurgan
Techgnostic
Male


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++)


--------------------
Ishmael
http://www.ishmael.org

Ron Paul 2008!
http://www.ronpaul2008.com/


Extras: Filter Print Post Top
Invisibleslacker008
Resident Psychiatrist
 User Gallery

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.


Extras: Filter Print Post Top
OfflineLegend9123
Male


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


Extras: Filter Print Post Top
InvisibleDieCommie

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)

:owned:


Extras: Filter Print Post Top
Jump to top Pages: 1


Similar ThreadsPosterViewsRepliesLast post
* Shroomery Radio Instructions. GabbaDjS 1,322 11 12/17/04 06:49 PM
by Kenny7822
* The Instructions from Hell SquattingMarmot 1,119 11 12/03/03 11:00 AM
by Phluck
* Instructions ShroomismM 868 9 09/02/04 05:03 PM
by Requiem
* Official Shroomery Shape-Up Program!
( 1 2 3 4 all )
Delyrium 8,878 70 05/04/04 10:36 PM
by utopianglory
* best program to convert video files to dvd format DeadPhan 1,157 7 04/10/05 10:12 PM
by oysterguy63
* What downloading program do you use?
( 1 2 all )
User Exists 3,408 24 12/21/02 09:45 PM
by matts
* what's a good program for instant messaging?
( 1 2 all )
Muppet 2,934 22 05/11/05 02:58 PM
by mantis
* music downloading programs YouEnjoyMyself 1,126 14 05/10/04 11:37 PM
by DannyT

Extra information
You cannot start new topics / You cannot reply to topics
HTML is disabled / BBCode is enabled
Moderator: Entire Staff
502 topic views. 7 members, 37 guests and 40 web crawlers are browsing this forum.
[ Show Images Only | Sort by Score | Print Topic ]
Search this thread:

Copyright 1997-2024 Mind Media. Some rights reserved.

Generated in 0.025 seconds spending 0.008 seconds on 14 queries.