0

i am trying to send a string that is "genrecode" back to my main function from the my other function known as char Vote. At first, i used the 'int' data type for the variable "genreCode" since its just integers but i wanted to see if i can send it as a string. How do i do that?

#include <stdio.h>

#include<string.h>

void View_Genre();

char Vote();

void View_Result(int, int, int);

void Exit(int, int , int);

int main ()
{
    int option;
    char genreCode[2];
    int rock=1;
    int pop=1;
    int country=1;
    int Trock=0;
    int Tcountry=0;
    int Tpop=0;
    
    printf("WELCOME TO MUSIC GENRE VOTING SYSTEM\n");
    
    printf("View Genre\n");
    printf("Vote\n");
    printf("View Result\n");
    printf("Exit\n");
    
    do
    {
        printf("\nInput option : ");
        scanf("%d", &option);
        
        switch(option)
        {
            
            case 1 : View_Genre();
                     break;
                     
            case 2 : genreCode=Vote();
                     if(strcmp(genreCode,"11")==0)
                     {
                        Trock=Trock+rock;
                     }
                     else if(strcmp(genreCode,"22")==0)
                     {
                        Tpop=Tpop+pop;
                     }
                     else if(strcmp(genreCode,"33")==0)
                     {
                        Tcountry=Tcountry+Tpop;
                     }
                     break;
                     
            case 3 : View_Result(Trock, Tpop, Tcountry);
                     break;
            
            case 4 :Exit(Trock, Tpop, Tcountry);
        }
    }while(option==1 || option==2 || option==3);
    
    printf("\nThank you.");
    return 0;
}

void View_Genre()
{
    printf("GENRE    CODE\n");
    printf("ROCK      11 \n");
    printf("Pop       22 \n");
    printf("Country   33 \n\n");
}

char Vote()
{
    char genreCode[2];
    
    printf("Enter genre code : ");
    scanf("%d", &genreCode);
    
    if(strcmp(genreCode,"11")==0)
    {
        printf("Your favourite genre is Rock");
    }
    else if(strcmp(genreCode,"22")==0)
    {
        printf("Your favourite genre is Pop");
    }
    else if(strcmp(genreCode,"33")==0)
    {
        printf("Your favourite genre is Country");
    }
    return genreCode;
}

void View_Result(int Nrock, int Npop, int Ncountry)
{
    printf("GENRE    VOTES\n");
    printf(" Rock      %d \n", Nrock);
    printf(" Pop       %d \n", Npop);
    printf(" Country   %d \n", Ncountry);
}

void Exit(int Nrock,int Npop,int Ncountry)
{
    char popular[10];
    
    if(Nrock!=0 || Npop!=0 || Ncountry!=0)
    {
        if(Nrock>Npop)
        {
           if(Nrock>Ncountry)
           {
            strcpy(popular,"Rock");
           }
        
           else
           {
            strcpy(popular,"Country");
           }
        
        }
        else if(Npop>Ncountry)
        {
        strcpy(popular,"Pop");
        }
        else
        {
        strcpy(popular,"Country");
        }
        printf("The most popular music genre is : %s\n", popular);
    }
    else
    {
        printf("\nYou have not vote any genre\n");
    }
}

The rest are all correct. I just need to find out how do i send back a string to my main function.

Tharshen
  • 1
  • 3
  • 3
    Does this answer your question? [Returning a C string from a function](https://stackoverflow.com/questions/1496313/returning-a-c-string-from-a-function) – Gerhardh Jun 18 '22 at 07:58

1 Answers1

0

You could do this by allocating space in the heap for your genreCode variable.

//...
#include <stdlib.h>
//...

int main ()
{
    // ...
    //allocate space for 3 chars in the heap
    //the third char stores the string terminator '\0'
    char* genreCode = malloc(sizeof(char)*3);
    // ...
    
    do
    {
        //...
        
        switch(option)
        {
            //...
            case 2 : Vote(genreCode);
                     //...
                     break;
            //...
        }
    }while(option==1 || option==2 || option==3);
    
    //...
    free(genreCode); //Free the allocated memory
    return 0;

}
 
//...

void Vote(char* genreCode)
{   
    printf("Enter genre code : ");
    scanf("%s", genreCode);
    
    //...
}

//...

Reference:

B1TC0R3
  • 131
  • 8
  • Thank you for pointing out my mistake, I have fixed these issues to the best of my ability. Let me know if there is anything else wrong with this code. – B1TC0R3 Jun 18 '22 at 08:46