-3

I have some trouble with the parameters (this is my first attempt of using two functions) when constructing the function and calling it.

Here is the debugger's error:

I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp||In function 'bool isPrime(int*)':|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|11|error: 'i' was not declared in   this scope|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|16|error: 'i' was not declared in   this scope|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp||In function 'int main()':|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|34|error: too few arguments to  function 'bool isPrime(int*)'|
I:\c++\11_Tombos feladatok-beszuras_torles\19.cpp|9|note: declared here|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

And here is the code:

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;

bool isPrime(int t[])
{
    for(int j=2; j<=sqrt(t[i]); j++)
    {
        if(t[i]%j==0)
            return false;
    }

    if(t[i]<2)
        return false;

    return true;
}

int main()
{
    int t[50], n, i;
    cout<<"Size of array: ";
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        cout<<i<<". Element: ";
        cin>>t[i];
    }
    i=1;
    while(i<=n)
    {
        if(isPrime())
        {
            for(int j=n; j>=i+1; j--)
            {
                t[j+1]=t[j];
            }
            t[i+1]=99;
            n++;
            i=i+2;
        }
        else
        {
            i++;
        }
    }
    for(int i=1; i<=n; i++)
    {
        cout<<t[i]<<", ";
    }
    cout<<"\n";
    system("pause");
    return 0;
}

I know that system("pause") is slow and it's not recommended etc. Don't blame me for using it (suggest something else instead).

Keeler
  • 2,102
  • 14
  • 20
L. Huni
  • 3
  • 1
  • You probably want to be including and if this is c++. Doesn't fix your problem though. – Brian Schlenker Jan 09 '14 at 20:45
  • 2
    So many issues with this code. The first thing you need to do is read the error messages. Start at the top one and fix it (`i` shouldn't even be in there, and you probably shouldn't be passing the array). The next one tells you that you need to supply a parameter when you call `isPrime`, otherwise it doesn't know what you're testing. If you're confused about all this, maybe take a step back and read a couple of chapters from a good book on C. – paddy Jan 09 '14 at 20:47
  • No, this is what I want to include (cmath does the same as math.h and I use cout and cin operands witch are not recognized by the compiler using cstdlib). Nothing is bad with the includes. Maybe I'm a beginner but not this much – L. Huni Jan 09 '14 at 20:52
  • cout and cin are in the iostream header... not sure how cstdlib would affect that – Brian Schlenker Jan 09 '14 at 20:58
  • sorry for that above, i realized later that it's another header. But it still didn't resolved it. – L. Huni Jan 09 '14 at 21:04

3 Answers3

3

Here's a code review. :)

  1. isPrime should take an int t parameter, not an array. You also use t[i] in this function, but i is out of scope! You declared i in main. Replace both t[] and t[i] with just t. Or, even better, call it number instead so it's super clear.
  2. You declare int i on the first line of main, and then again in the for loop directly beneath it that initializes the t array. I'm surprised this didn't cause a compilation error.
  3. In the for loops that start like for(int i = 1;..., you will miss the first element of the array every time! Remember: arrays are indexed from 0, so those should be for(int i = 0;....
  4. No error checking on user input (statement cin >> n in main). What if I enter 100? Your array t in main holds 50 integers, so t[100] would cause your program to crash with a segfault.
  5. In the while loop of main, the line if(isPrime()) won't compile because you defined isPrime to take a parameter, but you aren't calling it with any parameters. I think you meant if(isPrime(i))?
  6. In the for loop right below that, you initialize int j = n, then try to access t[j+1], which isn't allocated. This will also cause your program to crash.
  7. Why do you have n++ in the while loop? I can see this causing problems/unintended behavior because this is the bound on your while loop.
  8. When writing C++ code, it's good practice to use the C++-style headers. So instead of #include <stdlib.h> use #include <cstdlib>, and instead of #include <math.h> use #include <cmath>.

There are probably a few more bugs/issues, but that should get you moving. Let us know what you come up with!

Keeler
  • 2,102
  • 14
  • 20
2

The debugger's error tells you exactly what is wrong!

  1. i does not exists in method isPrime(). Just because you have an i in the main(), you can not use it in another function (read about Scope)
  2. You call the isPrime() function without a parameter.

I suggest you start with easier programs, and also read about functions.

Verena Haunschmid
  • 1,252
  • 15
  • 40
  • All you wrote is my fault. I know about the scope, but thecode what i copied there was oneof my tries to resolve the prolem – L. Huni Jan 09 '14 at 21:08
-2

You should use something like

cin.ignore();

instead of system("PAUSE");

Tiit
  • 185
  • 2
  • 8