Page 1 of 4 1 2 3 ... LastLast
Results 1 to 20 of 78
  1. #1
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    BG Programmers (Homework help)

    This is my first C++ class, and I'm having trouble finding the mistake in this code. It's a simple program on how to check if the inputed number is prime or not prime.

    Code:
    #include <iostream>
    using namespace std;
    int a;
    
    int c (a)
    {
        if (c=1){;
        return c;
        }
        int c = c -1;
    }
    
    double b(int a)
    {
        if (a % c !=0){
        cout << "The number " << a << "is not prime.";
        }
        else{
        cout << "The number " << a << "is prime!";
        }
    }
    
    int main (){
        int a;
        cout << "Please enter a number: ";
        cin >> a;
        b(a);
        return 0;
    
    }
    The error I'm getting is
    Line 6, error: expect unqualified-id before '{' token.

    I really can't find anything wrong here, can someone please help me out?

  2. #2
    Hydra
    Join Date
    Apr 2008
    Posts
    119
    BG Level
    3

    change
    if (c=1){;
    to
    if (c==1){
    take out the semicolon and add an extra =

  3. #3
    CoP Dynamis
    Join Date
    Oct 2006
    Posts
    286
    BG Level
    4

    Line 7, "if (c=1){;" should be "if (c==1){". = is the assigning operator, == is the equal to operator in C++.

    Edit: beaten

  4. #4
    Canadian Fury
    MANITOBA IS NOT A REAL PLACE

    Join Date
    Jun 2008
    Posts
    1,089
    BG Level
    6
    FFXI Server
    Valefor

    Been a while since I wrote C++, but maybe try writing int c(a) instead of int c (a) ?

  5. #5
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Took out the semi-colon, getting "line 6, expected ',' or ';' before '{' token. Thanks for the catch on the double equal signs, forgot about that.

  6. #6
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Still need a bit of help. I've been trying everything, looking over all my notes and nothing. The program is supposed to take an interger from the user in a recursion and divide itself until it reaches 1.

  7. #7
    Sea Torques
    Join Date
    Oct 2006
    Posts
    566
    BG Level
    5
    FFXI Server
    Sylph

    I'm not sure if your teacher has gone over any standards or common practices, but there are few things you should not be doing, mostly for maintainability but also because it makes the code much more complicated to read.

    Code:
    #include <iostream>
    using namespace std;
    int a;
    There's no reason to define this variable here. When testing primes, everything can be done within a single method's scope.

    Code:
    int c (a)
    {
        if (c=1){;
        return c;
        }
        int c = c -1;
    }
    A method definition includes the return type, method name, and method parameters. Here, you've given a return type (int), method name (c), and a parameter without a type (a). You're then testing a variable that will be undefined and returning it or creating a new variable defined to be a function minus one. A lot more complicated than you need to be doing for this problem.

    Code:
    double b(int a)
    {
        if (a % c !=0){
        cout << "The number " << a << "is not prime.";
        }
        else{
        cout << "The number " << a << "is prime!";
        }
    }
    The same applies here. You're testing things that don't exist and mixing variables and methods.

    A simple solution for some of these issues is to name your method and variables properly. Single letter names are extremely unhelpful, unless they're used in a very limited scope. Using i for an index or incrementer is ok, but when you're using it to define a function, you're going to run into trouble.

    I'm not sure what the scope of the actually homework problem is (it almost looks like you were attempting to use recursion), but here's a sample of what I would do:

    Code:
    #include <iostream>
    
    bool isPrime(int number)
    {
    // Return true if prime, false if not prime.
    }
    
    int main ()
    {
        int input;
        std::cout << "Please enter a number: ";
        std::cin >> input;
    
        if (isPrime(input))
        {
            std::cout << "Number is a prime.";
        }
        else
        {
            std::cout << "Number is not a prime.";
        }
    
        return 0;
    }

  8. #8
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Yeah, the homework is about creating a recursion. I only used single letter names because it's simpler for me to read. I'll replace them with their proper names once I'm done and make sure this works properly.

    These are the instructions.

    Spoiler: show
    Write a C++ program to test if a number is prime. Create a function called isPrime that takes an integer called testNum and returns 1 if testNum is prime, 0 if testNum is not prime. Your main function should prompt the user for a number, call the isPrime function, check the return value, and print either "The number _ is prime!" or "The number _ is not prime." (where _ is the number the user entered)

    A few hints: You will need at least two different functions: the isPrime function that gets called from main, and a second function that isPrime will call that takes the original testNum along with the recursively decreasing number to do the division check. This second function should call itself recursively. Your Programming 1 project this week tested if a number was evenly divisible by something - use the same concept here for checking divisibility.

    Call your isPrime function from main after prompting the user for a number to check. For example, if the user enters 7, the isPrime function should return 1 and you can print "The number 7 is prime!" and if the user enters 4, it should return 0 and you can print "The number 4 is not prime." Try running your program plenty of times using different numbers to make sure it works correctly.

  9. #9
    Sea Torques
    Join Date
    Oct 2006
    Posts
    566
    BG Level
    5
    FFXI Server
    Sylph

    Quote Originally Posted by Aristio View Post
    Still need a bit of help. I've been trying everything, looking over all my notes and nothing. The program is supposed to take an interger from the user in a recursion and divide itself until it reaches 1.
    Since it appears that you're actually having trouble with the maths part as well as the coding part, I'll provide a bit of additional information.

    To find a prime number, you need to see if it is divisible by anything other than one and itself. To do this recursively, you need a function that is able to take a number and divide it until it finds a zero remainder (or a divisor of zero). To do this, you'll want to use a function of the form:

    Code:
    bool isPrime(int number, int divisor);
    (In practice, this could be two functions, one that takes the number only and calls another function that takes the number and the number-1, but I find it simpler to do in a single call). When you first call this function, you'll want the divisor to be one less than the number (there are ways to optimize this further if you look into the math of prime numbers, but it makes the code more complicated). This means that you'll first call the function like so:

    Code:
    isPrime(input, input - 1)
    What does isPrime actually do, though. We know that if the divisor is one, the number will be divisible by one, so we should return true, because the number is a prime.

    Code:
    bool isPrime(int number, int divisor)
    {
        if (divisor == 1)
        {
            return true;
        }
    }
    This function isn't recursive and will only return true if the number is 2, so to make it recursive, it has to call itself.

    Code:
    bool isPrime(int number, int divisor)
    {
        if (divisor == 1)
        {
            return true;
        }
        return isPrime(number, divisor - 1);
    }
    We call the isPrime function with the same number, but a divisor that is one less. This means that each time the isPrime function gets called, the divisor is decreased by one. In this case, it will always return true (assuming that number was greater than one) because eventually the isPrime function will return true, and true will return all the way up the stack (if your teacher hasn't gotten to this yet, I'm sure they will).

    We know that we have another condition: if number divided by divisor is zero, then the number is not a prime. We add an else clause instead of returning the isPrime call. This checks that our remainder is non-zero. If it is zero, we return false (meaning the number is not prime). If it is not zero, we call the isPrime function again.

    Code:
    bool isPrime(int number, int divisor)
    {
        if (divisor == 1)
        {
            return true;
        }
        else
        {
            if (number % divisor == 0)
    	{
                return false;
            }
            else
    	{
                return isPrime(number,divisor-1);
    	}
        }
    }
    Obviously, this code has no error handling (calling the function with a number <= 1, 1-1 as parameters will crash) and it not optimized, but it should give you an idea about what the teacher was looking for.

  10. #10
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Just a quick question Tidane, we've only been using int and double, what is a bool?

  11. #11
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    bool is Boolean. It can either be 1 or 0.

  12. #12
    Onto plan B...
    Join Date
    Aug 2008
    Posts
    913
    BG Level
    5
    FFXI Server
    Sylph

    Bool is a type that is either true (1) or false (0).

  13. #13
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    Quote Originally Posted by Aristio View Post
    Code:
    int c (a)
    {
        if (c=1){;
        return c;
        }
        int c = c -1;
    }
    Tidane probably gave you most of what you need to know, but since I didn't see it directly mentioned, this is what your "Line 6, error: expect unqualified-id before '{' token." error means:

    The unqualified id is 'a'. You're passing 'a' into function 'c', but what is 'a'? 'a' has no type. The compiler hit the opening bracket signifying the start of the 'c' function and it turned to you and said "Whoa dawg, what is 'a'?"

    I don't really understand what you were trying to achieve with this function in particular even after reading your assignment prompt, but hopefully since I'm posting this two hours late, you have something else in place.

    Spoilered my original post after reading the whole lot of what Tidane wrote. Read it if you want.
    Spoiler: show
    Ignoring the c=1 -> c==1 part, since that was already addressed...

    First problem I see is that you're passing 'a' to function 'c'. What is 'a'? You must define a type for 'a'. No, it will not use your previously-defined 'a' variable here. However, the compiler probably won't generate an error here and will assume that you meant 'a' was an integer. It should generate a warning though.

    I don't know if the question got answered somewhere in the part where you guys got off on a tangent, but if this is the code verbetim, then your problem is that your function name is 'c' and you can't say 'c == 1' when 'c' is your function name. Then you return 'c'? You can't return 'c' because its your function name. Well, technically you could, but I'm sure that's not what you intended to do because you'd be returning the memory address of your funct--you know what, lets just say you can't return a function name. The compiler will also throw a fit when it gets to the point where you've declared 'c' as an integer, because it's already identifying 'c' as your function name.

    If you imagine the function as a grocery store and the variables as items in the grocery store, then the if statement that I quoted would read something like this: Take 'a' to the store and come back with an item (line: int c(a) ). If the store is one (line: if(c == 1) ), then come back with the store (line: return c;). That makes no sense to you for the same reason that it makes no sense to the compiler.

    Sounds like your main problem is that your variables and functions share the same naming convention and you're confusing the hell out of your compiler. There are several more problems with your code besides that, but to answer just your question and not do your actual homework problem, this should be sufficient to get you started.

  14. #14
    Anti-crusade crusader
    Join Date
    Oct 2008
    Posts
    1,438
    BG Level
    6
    FFXI Server
    Asura

    Quote Originally Posted by Raldo View Post
    I don't really understand what you were trying to achieve with this function in particular even after reading your assignment prompt, but hopefully since I'm posting this two hours late, you have something else in place.
    I have tried to rebuild this 6 different times. Recursions are indeed troublesome. Still going over notes, comments in this thread, and internet searches though.

    I guess where I'm getting lost is the return values.

    I understand that

    Code:
    int somename (int x, int y)
    {
        int y = 8;
        int x = 7;
        somename = x +y;
        return somename;
    }
    It will print out 15, but how do I get "a function called isPrime that takes an integer called testNum and returns 1 if testNum is prime, 0 if testNum is not prime"? How exactly do I return 1, then make 1 print out "X is not prime"?

  15. #15
    wotg torrent kitty :3
    Join Date
    Jun 2007
    Posts
    1,624
    BG Level
    6

    um..that's what booleans are for. they return false (0) or true (1) like mentioned before. depending on the return, you can output the requested sentence. tidanes' solution on his first post is exactly how to solve this. from the sounds of it, you probably should pick up some guides/book about programming basics, as this isn't anything c++ specific apart from the terminology.

  16. #16
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    Aristio already said they haven't got to booleans, so just do it without those. I don't know why you guys are pushing booleans. The instructions say return 1 or 0, so you end up with something that looks like this:
    Code:
    int recursiveFunc(int testNum, int divisor)
    {
        // If divisor is 1, return 0
        // Check to see if testNum is divisble by divisor
        // If it is, return 1
        // If it isn't, subtract 1 from divisor and call recursiveFunc
    }
    
    int isPrime(int testNum)
    {
        int divisor;
        // Calculate what your starting divisor should be
    
        return recursiveFunc(testNum,divisor);  // Call recursive function
    }
    
    int main(int testNum)
    {
        int a;
        cout << "Please enter a number: ";
        cin >> a;
        int primeCheck = isPrime(a);
        if(0 == primeCheck)
        {
            std::cout << "Number is a prime.";
        }
        else
        {
            std::cout << "Number is not a prime.";
        }
        return 0;
    }
    I've left most of the actual work out, using the good code from the OP while trying to follow the assignment instructions to provide a general layout.

    Related, but off-topic...
    Spoiler: show
    I feel the need to correct your example code here...
    Code:
    int somename (int x, int y)
    {
        int y = 8;
        int x = 7;
        somename = x +y;
        return somename;
    }
    x and y were already defined because they were passed into the function, you do not need to re-define them.
    Code:
    int somename (int x, int y)
    {
        y = 8;
        x = 7;
        somename = x +y;
        return somename;
    }
    I could go on to say that passing x and y into the function serves to purpose, since you're not using them... turning the function into this:
    Code:
    int somename()
    {
        int y = 8;
        int x = 7;
        somename = x +y;
        return somename;
    }

  17. #17
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    Quote Originally Posted by Raldo View Post
    Aristio already said they haven't got to booleans, so just do it without those. I don't know why you guys are pushing booleans.
    Because it's the proper way to do it, and it's incredibly simple. It's not like anyone is asking him to make some complex change that a professional programmer would have difficulty with. It's a variable type. If he can't understand that bool has possible values of 1 or 0, and how to declare it, he should probably just drop the class now.

  18. #18
    D. Ring
    Join Date
    Nov 2011
    Posts
    4,998
    BG Level
    7
    FFXIV Character
    Raldo Volca
    FFXIV Server
    Balmung

    Quote Originally Posted by hey View Post
    Because it's the proper way to do it, and it's incredibly simple. It's not like anyone is asking him to make some complex change that a professional programmer would have difficulty with. It's a variable type. If he can't understand that bool has possible values of 1 or 0, and how to declare it, he should probably just drop the class now.
    Proper and simple or not, the instructions specifically say to return a 1 or a 0, not a true or a false. As far as memory and performance is concerned they are exactly the same thing. You are merely making this more complex than it has to be for someone who is only starting out.

    What I would be questioning is why a teacher would ever teach recursive functions before teaching what a boolean is, but that's neither here nor there.

  19. #19
    hey
    hey is offline
    listen!
    Join Date
    Apr 2011
    Posts
    7,234
    BG Level
    8
    FFXI Server
    Sylph

    Quote Originally Posted by Raldo View Post
    Proper and simple or not, the instructions specifically say to return a 1 or a 0, not a true or a false. As far as memory and performance is concerned they are exactly the same thing. You are merely making this more complex than it has to be for someone who is only starting out.

    What I would be questioning is why a teacher would ever teach recursive functions before teaching what a boolean is, but that's neither here nor there.
    Changing int to bool is not making anything more complex.

  20. #20
    Onto plan B...
    Join Date
    Aug 2008
    Posts
    913
    BG Level
    5
    FFXI Server
    Sylph

    I don't think anyone was pushing bool on him... lol. And seriously, the question is basically asking for a bool return type.

    Aristio, look at it this way: bools return a true or a false. That can be seen as 1 for true and 0 for false. Think of your function as a question. "Is integer x prime?" "Yes" = true = 1 or "No" = false = 0. That being said, you could even return an integer of 1 or 0 for the same effect. Also, bit of advice, try to make your function names and variables descriptive. It's good practice to try to be consistent with naming conventions. That is, don't name a variable ThisIsAnInt and then name something else like thisisastring. First word lowercase and then capitalizing the first letter of every word after is pretty common for variables. All first letters capped is pretty common for functions, also.

    Side note: Raldo, you do realize that when printing a bool, it gives a 1 or 0?

Page 1 of 4 1 2 3 ... LastLast

Similar Threads

  1. BG Homework Help (Grammar)
    By aduidarnenye in forum General Discussion
    Replies: 8
    Last Post: 2011-01-17, 06:36
  2. BG Cooks - Grilling Help Please. :(
    By Klutz in forum General Discussion
    Replies: 82
    Last Post: 2010-02-17, 17:25
  3. BG Legal Advice: HELP ME OUT!
    By Synbios in forum General Discussion
    Replies: 7
    Last Post: 2010-02-15, 04:00
  4. BG advice: Kitty help
    By Apelila in forum General Discussion
    Replies: 5
    Last Post: 2009-11-23, 11:52
  5. Paging Dr. BG, pregnancy scare??? help!!
    By Nitsuki in forum General Discussion
    Replies: 123
    Last Post: 2009-05-29, 17:13
  6. Replies: 29
    Last Post: 2008-06-06, 11:15
  7. BG Audiophiles... lil help?
    By Doombear in forum General Discussion
    Replies: 29
    Last Post: 2008-05-22, 11:28
  8. My homework HELP ME
    By Endo in forum General Discussion
    Replies: 48
    Last Post: 2005-10-09, 10:33