what is wrong?here...help

This is a discussion on what is wrong?here...help within the C++ Programming forums, part of the General Programming Boards category; heres the code primeseries but it doesnt work #include<iostream> #include<conio> void primeseries() { for(int m=2;m<10;m++) { for(int i=2;i<=m;i++) { for(int ...

  1. #1
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    what is wrong?here...help

    heres the code primeseries but it doesnt work

    #include<iostream>
    #include<conio>
    void primeseries()
    { for(int m=2;m<10;m++)

    {

    for(int i=2;i<=m;i++)
    {

    for(int j=1;j<i;j++)

    {

    if (( m % i == 0 ) && ( i * j == m ))
    {cout<<m;}
    }
    }


    }


    getch();}



    void main(){
    primeseries();
    }
    help
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  2. #2
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    What's the error? And next time you paste codes.. use code tags...
    what does signature stand for?

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    this code compiles:

    Code:
    #include<iostream.h> 
    #include<conio.h> 
    
    void primeseries() 
    { 
    	for(int m=2;m<10;m++) 
    	{ 
    		for(int i=2;i<=m;i++) 
    		{ 
    			for(int j=1;j<i;j++) 
    			{ 
    				if (( m % i == 0 ) && ( i * j == m )) 
    					cout<<m; 
    			} 
    		} 
    
    	} 
    } 
    
    int main()
    { 
    	primeseries(); 
    	return 0;
    }
    Note that you should rather use int main() than void main().

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you wanted to use <iostream> then include the following line in your code:

    using namespace std;

    then it should work.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    newbie,

    First, aside from Ruski's admonition about the code tags , when using <iostream>, you must either write 'std::cout', or you must place 'using namespace std;' below your 'includes'.

    You can get away with 'cout' if you write your 'include' thusly: #include <iostream.h>. (Note the .h extension.)

    Second thing, you must add the .h extension to <conio> in order to use "getch();". <conio.h> is not part of the standard namespace, so you have to rely on the "old" header file designated by the .h extension.

    Third, place 'm % i' and 'i * j' in parentheses inside of your IF statement. Yes, someone will tell you that it's not necessary because of the "precedence" of the math operators over the "equal to" relational operator (==), but it's better form and easier to read.

    Last, getting back to Ruski's point, let us know what the error is when you post questions of this type. Your program is small enough to see the obvious problems, but in a larger program, they're not nearly as apparent.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    And the best way is to get a book. There are free ones online
    what does signature stand for?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21