Thread: problem with the output part whenever I use the "View All Topics" from the menu();

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    18

    Question problem with the output part whenever I use the "View All Topics" from the menu();

    After I entered two topics using the case '1' , I used the case '5'. After seeing the display, the category and the term show the right user input, but the description and the value always shows the second set of topic. I tried finding ways to fix it but nothing seems to work.

    The input is like this:
    My first set of topic:

    term: 1
    category: 2
    description: 3
    value: 4

    My second set of topic:

    term: a
    category: b
    description: c
    value: d

    The output went something like this:
    I went to the "View All Topics" part and this is what I got:

    Term: 1
    Category: 2

    Description: c <----- This should be 3

    Value: d <---- This should be 4


    Can you guys help me with this part? I would be really thankful if someone helps me fix this mess..



    Code:
    #include <stdio.h>
    #define numtopic 10 /*This is the number of topics*/
    /*I realized that "unknown escape sequence" will
    show up if I added space and "/"*/
    struct infopairs/*Max up to 15 information pairs
    user should not be asked how many information pairs will be inputed.*/
    {
       char datadesc[50];/*Can be more than 1 word*/
       char dataval [150];/*Only one word needed*/
    };
    struct theTopic/*This one consists of the following:
    Term, Category and Information Pairs. This structure will
    be used to add a topic.*/
    {
       char term[50];/*More than 1 word, 50 characters*/
       char category[50];/*More than 1 word, 50 characters*/
    
    
       struct infopairs descval;/*I called this desc for description
       and val for value*/
    };
    void descvalprint(struct infopairs *info, int count)/*This function is made so that
    I can check if I am really accepting the right kind of inputs from user. count
    is made in order to know the number of information pairs the user entered.*/
    {int i;
       for(i = 0; i < count; i++)
       {
           printf("Description:  %s\n\n", info->datadesc);
           printf("Value:   %s\n", info->dataval);
       }
    }
    void swap(char *a, char *b)/*swap(); function is made in order to switch
    the value of the 2 variables. I used a pointer because a without a pointer,
    there will be a "pass by value" only*/
    {char c;/*'c' will be used as a tool in order to switch the values*/
        c = *a;/*c takes the value of A*/
       *a = *b;/*a takes the value of B*/
       *b = c;/*b takes the value of c*/
    }
    void alphorder(struct theTopic *info, struct theTopic *info2)
    /*This function is supposed to make the disorganized strings into an
    albphabet order. 65-90 is A-Z.97-122 a-z ascii. info is the first
    string while the second string is the info2. These 2 will be compared and
    if the info's term is larger than the info2's term, then the info and
    info2 will be switched.*/
    {
    
    
    }
    char userview(struct theTopic *info, struct infopairs *descval, int count)
    /*This one will be used for the "View Topics" part. This one is supposed to
    show the user what they had typed before. The count is used in order to
    know how many information pairs are entered.*/
    {
       int i;/*For the for loop in descval to show the (count) pairs.*/
       char choice;/*This will be the choice of the user if he/she wants
       to go Next, Back or Exit. This will be returned.*/
    
    
       printf("Term:  %s\n", info->term);/*Show the term*/
       printf("Category: %s\n\n\n", info->category);/*Show the category*/
    
    
       for(i = 0; i < count; i++)/*T\his is the for loop. Its job is to
       show ALL the the inputs to the user. This will show all of
       the description and the values.*/
           descvalprint(&descval[i],count);/*The descval will send its
           address to descvalprint along with count.*/
    
    
       printf("Enter [N] Next\n"/*This is the question if they want to
       exit, Next, or back.*/
              "      [P] Previous\n"
              "      [0] Exit\n");
       scanf("%s", choice);/*This doesn't need to have an '&'
       because I am getting a string here.*/
    
    
       /*While choice is not of the following letters*/
       while(choice != 'N'||choice != 'n'||
             choice != 'P'||choice != 'p'||choice != '0')
             {
             printf("You just entered the wrong input!\n");
             scanf("%s", choice);/*This doesn't need to have an '&'
             because I am getting a string here.*/
             }
    
    
       system("pause");
    
    
       return choice;
    }
    void viewcat(struct theTopic info)/*This function is supposed to
    let the user view what the user previously entered. They should be
    able to see*/
    {
    
    
    }
    int infotopic(struct infopairs *info, int count)/*I made this function so that
    I can let the user input up 15 pairs. I also added the count so that I
    can tell the user how many times they already entered their stuffs*/
    {
        int stop;/*terminates this function*/
    
    
        if(count > 0)/*If count is still zero, or if this is the first time
        you run this function since the last input style is scanf*/
            gets(info->datadesc);/*Takes more than one string*/
    
    
        printf("Enter the description #%d:\n", count+1);/*Ask for the description.*/
        gets(info->datadesc);/*Gets more than 1 string*/
    
    
        printf("Enter the value #%d:\n", count+1);/*Tell user to enter values*/
        scanf("%s",info->dataval);/*Get only 1 string input.*/
    
    
        printf("Enter [0] to stop.\n");/*Ask if want to stop*/
        scanf("%d", &stop);/*Get input*/
    
    
        if(stop == 0)/*If the stop is zero then it will return a 0*/
        {
            system("cls");
            return 0;/*I used return 0; so that I can go back to the menu*/
        }
    }
    void addTopic(struct theTopic *info)/*This one is for adding a topic.
    Called the theTopic struct and named it as "info". I put the pointer
    so that it will not be "passed by value".*/
    {
       char choose;/*Choice of user*/
       char dump;
       int stop;/*terminates this function*/
    
    
       system("cls");/*It means that the system will clear the screen. I
       only wanted to clear the screen once here.*/
    
    
        printf("                          Adding Topic\n");/*This will be shown to users.*/
        printf("\n");/*space*/
    
    
    
    
        gets(info->term);/*I must separate this gets(); from the others,
        because I will not use this one like a normal gets();. It is because
        the users cannot input the term if the first gets is used for
        inputing anything (WHICH IS WEIRD).*/
    
    
    
    
        printf("Please enter the term:  \n");/* Tell user to enter term.*/
        gets(info->term);/*Get more than 1 string input.*/
    
    
        printf("Enter the category:     \n");/*Ask for the category.*/
        gets(info->category);/*Get more than 1 string input.*/
    
    
    
    
    }
    void menu()/*This shows the user the menu but this
    is not the main menu*/
    {
       struct theTopic info[numtopic];/*Declare info, info is a structure. There
       will be a maximum of ten topics.*/
       struct infopairs descval[15];/*Declare descval in order
       to get 15 pairs of information in the function addTopic.*/
    
    
       int cnt = 0;/*This one counts how many the user can add topics*/
       int count;/*15 is the max. This one counts the number
       of information pairs.*/
    
    
       int i;/* I used "i" to take the value of infotopic(&descval[15]);*/
    
    
       char NPEchoice;/*This variable is going to take the return value of
       function userview();*/
       char choice;/*the choice of the user from the menu. I used
       char because when the user gets it wrong, it only resets.*/
    
    
       count = 0;/*initialize count to zero to prevent garbage value..*/
       do
       {
       /*This is the menu.*/
       printf("          Quiz Bee\n"
             "____________________________\n"
             "Choose the option:\n"
             "[1] Add Topic\n"
             "[2] Add Information\n"
             "[3] Delete Topic\n"
             "[4] Delete Information\n"
             "[5] View All Topics\n"
             "[6] View By Category\n"
             "[7] View Specific Term\n"
             "[8] Export\n"
             "[9] Import\n"
             "[0] Exit\n");
    
    
       scanf("%c", &choice);/*Get user's choice. I used character
       in order to have a more stable program when the user enters
       numbers and letters. Using integers tend to give problem to
       the program.*/
    
    
       /*User's choice will have outcomes by using switch*/
       switch(choice)
       {
          case '1':
            if(cnt < numtopic)/*The number of topics at maximum is 10*/
            {
                system("color 2b");/*Nothing special, just colors and designs.*/
    
    
                addTopic(&info[cnt]);/*addTopic will be used if 1 is entered.
                Address of info will be passed on to the addTopic. The "cnt"
                is very vital here. It can keep up to 10 topics.*/
                cnt++;/*Add the cnt by 1.*/
    
    
                count = 0;/*initialize count to zero to prevent garbage value..*/
                do
                {
    
    
                   i = infotopic(&descval[count],count);/*infoTopic is needed so that
                   entering 15 pairs is possible. The value infotopic
                   returns will be put to "i".*/
    
    
                   count++;/*I need this in order to gain access of the 15 arrays*/
                }while(i != 0 && count < 15);/*While i is not zero adn while
                you keep adding the count, you don't get 15 yet..*/
            }
            else
            /*Use this if user has entered the maximum nsumber of topics*/
                printf("You have already have %d topics!\n", numtopic);
                system("pause");
                        break;
              case '2': break;
              case '3': break;
              case '4': break;
         case '5':
    
    
            system("cls");
    
    
            NPEchoice = userview(info,descval,count);
    
    
    
    
                        break;
              case '6': break;
              case '7': break;
              case '8': break;
              case '9': break;
              case '0': break;
              default: system("cls");/*Clear the screen
                       when user is wrong.*/
       }
    
    
       }
       while(choice != 0);
    }
    void NPE(char NPEchoice,int count,int cnt)/*NPE stands
    for next, previous, and exit. This is going to take
    the choice and will control the values of count and
    cnt. That way, I can control the pages of what the
    user entered. cnt is for the number of topics. count
    is the number of information pairs.*/
    {
       switch(NPEchoice)
       {
           case 'N':count++;
                    cnt++;
           case 'n':break;
    
    
           case 'P':count--;
                    cnt--;
           case 'p':break;
    
    
           case '0': menu();
                     break;
       }
    }
    int main()
    {
       menu();
    
    
       system("pause");/*pauses system*/
       return 0;/*Returns a 0*/
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Just something I noticed real quick
    Code:
    char choice;
    scanf("%s", choice);
    choice should be an array if you are entering in a string.

  3. #3
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Some advices without testing your code.
    1. Do not hijack the code with comments
    2. Use curly brackets to if-else statements. Only identation doesn't work. if/else without curly brackets nests a single command.
    3. Isolate menu and play. Parse all possible user inputs and just print the output to check if input corresponts to output.

    int main(void) ... return 0; //Spoiler
    Last edited by ch4; 11-03-2012 at 08:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 07-17-2012, 09:02 AM
  2. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM
  3. Output "problem"
    By Willdud in forum C++ Programming
    Replies: 2
    Last Post: 10-31-2005, 01:46 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM