Thread: Homework Help....not wanting answers just a point in the right direction.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Homework Help....not wanting answers just a point in the right direction.

    First As stated I am not looking for answers I would just like some pointers on why it is not working like I think it should almost some "Mentorship". Thanks in advance.

    Assignment:
    Write a main function to input 20 integers in the range of 1 to 6. Write a function to count the number of times the numbers 2 and 5 occur. The function should declare static variables count2 and count5. Check the data validity in the main function.

    My Issue:

    My issues are
    I'm not printing the counted numbers cont2 or cont5 when the program ends
    and I tested the funccountnum and it doesnt seem to get to the else if(Num = 5) part.

    I am using Microsoft Visual Studio 2010 express.
    Code:
    #include<stdio.h>
    #include<iostream>
    void funccountnum(int, int, int);
     
    int main(){
    
    int Num = 0;
    int cont2 = 0, cont5 = 0;
    int p;
    for (p = 0; p < 21; p++){
    printf("Enter a number between 1 and 6: ");
    scanf("%d", &Num);
    if (Num <= 6){
    funccountnum(Num, cont2, cont5);
    }
    elseif(Num >= 7){
    printf("Please input a valid number!\n");
    }
    }
    printf("\n");
    printf("Times 2 was selected\tTimes 5 was selected\n\n");
    printf("%d\t%d\n\n", cont2, cont5);
    system("PAUSE");
    return 0;
    }
    void funccountnum(int Num, int cont2, int cont5){
    staticint count2 = 0;
    staticint count5 = 0;
    if(Num = 2){
    count2++;
    }
    elseif(Num = 5){
    count5++;
    }
    cont2 = count2;
    cont5 = count5;
    return; 
    }
    Last edited by Salem; 10-22-2011 at 11:35 AM. Reason: replaced dog-food HTML code paste from VS

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    #include<stdio.h>
    #include<iostream>
    Well the first thing to do is decide whether you're programming in C or C++.

    Next, you're using = where you should be using ==

    Last, you need to pass pointers to variables to get a result back from a function (or use a reference in C++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Decide if you are programming C or C++

    "elseif" is NOT a c/c++ keyword.

    Learn the difference between "=" and "=="

    Learn to indent your code! I made it compile; but, it still has bugs!
    Code:
    #include <stdio.h>
    void funccountnum(int, int, int);
    
    int main()
    {
    
        int Num = 0;
        int cont2 = 0, cont5 = 0;
        int p;
        for (p = 0; p < 21; p++)
        {
            printf("Enter a number between 1 and 6: ");
            scanf("%d", &Num);
            if (Num <= 6)
            {
                funccountnum(Num, cont2, cont5);
            }
            else if(Num >= 7)
            {
                printf("Please input a valid number!\n");
            }
        }
        printf("\n");
        printf("Times 2 was selected\tTimes 5 was selected\n\n");
        printf("%d\t%d\n\n", cont2, cont5);
        system("PAUSE");
        return 0;
    }
    void funccountnum(int Num, int cont2, int cont5)
    {
        static int count2 = 0;
        static int count5 = 0;
        if(Num = 2)
        {
            count2++;
        }
        else if(Num = 5)
        {
            count5++;
        }
        cont2 = count2;
        cont5 = count5;
        return;
    }
    Tim S.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Salem and Tim,

    Thanks that "==" was the key to the the static int working. Now I just have to figure out how to get the cont2 and cont5 to pass back their values to the main() for output.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Got it, thx for the help it is now running correctly pointers will be added to the tool kit....lol

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    I am also now seeing the humor in the fact I was asking for some pointers and it actually was a pointer that was key.......haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MDI App help - point me in right direction
    By Noose in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2005, 04:21 AM
  2. need a point in the right direction
    By sweetly in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2003, 05:19 PM
  3. Point me in the right direction
    By RealityFusion in forum C++ Programming
    Replies: 17
    Last Post: 08-22-2003, 05:05 AM
  4. can someone point me in the right direction?
    By bean583 in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 10:35 PM