Cần giúp đỡ code menu

Em mới bắt đầu học lập trình ngôn ngữ C được 1 tuần và em không hiểu lắm về con trỏ.

Và hôm nay em cần sự trợ giúp của các anh/chị sửa giúp em bài menu này, em đã hỏi nhiều người để giúp em và tất cả bọn họ cứ chỉ nói với em duy nhất đúng một câu “tự đi mà làm”, nhưng em không hiểu về con trỏ thì sao mà tự làm được :frowning: . Nên em mới cần sự trợ giúp của các anh/chị trong diễn đàn này ạ. Mong anh/chị giúp và sửa bài menu này cho em, mong em sẽ xem và hiểu được vấn đề ạ :slight_smile:

Cái menu này của em,bị lỗi ở phần Question 2.

  • Question 2 của em gồm 3 option tính toán S.
  • Đề bài menu yêu cầu nhập n đúng một lần duy nhất để có thể sử dụng giá trị n đó để làm được cả 3 options mà không cần nhập lại lần nữa.
    –> Cái này thì nó khó hơn là chọn option nào thì nhập n option đó, chuyển sang option khác thì nhập n khác. Và hơn nữa, em vẫn không hiểu về con trỏ để làm được phần này.
  • Các hàm trong switch case Question 2, nó không nhận giá trị n mà em nhập vào ở bên ngoài switch case. Thay vào đó, nó cứ cho n = 0 sẵn và vì vậy cho nên S ở option nào đều luôn luôn = 0.
    –> Em muốn làm thế nào để các hàm tính toán S đó, nó nhận được giá trị n mà em nhập vào ở bên ngoài switch case.
#include <stdio.h>
#include <stdlib.h>

void CalculatePlusToN(){
	int n,i;
    float S;
    S = 0;
	for(i = 1; i <= n; i++)
    {
        S = S + i;
    }
    printf("\n==> S = %0.0f\n", n, S);
}

void CalculateOneDivideOdd(){
	int n,i;
    float S = 0;
    for(i = 1;i <= n;i++)
    {
    	if (i%2==1){
    		S +=1/(float)i;
		}else{
			S += 0;
		}
    }
    printf("\n==> S = %0.0f\n", n, S);
}

void CalculateOneDivideEven(){
	int n,i;
    float S = 0;
    for(i = 1;i <= n;i++)
    {
    	if (i%2==0){
    		S +=1/(float)i;
		}else{
			S += 0;
		}
    }
   printf("\n==> S = %0.0f\n", n, S);
}

void NameAndDaysOfTheMonth(){
	int month;
	printf ("Enter the month: ");
	scanf ("%d",&month);
	if (month >=1 && month <=12){
		if (month == 1){
			printf ("The name of the month is : January\nThe number of days of the month is : 31\n");
		}
		if (month == 2){
			printf ("The name of the month is : February\nThe number of days of the month is : 28 (29 if the year is the leap year)\n");
		}
		if (month == 3){
			printf ("The name of the month is : March\nThe number of days of the month is : 30\n");
		}
		if (month == 4){
			printf ("The name of the month is : Arpil\nThe number of days of the month is : 30\n");
		}
		if (month == 5){
			printf ("The name of the month is : May\nThe number of days of the month is : 31\n");
		}
		if (month == 6){
			printf ("The name of the month is : June\nThe number of days of the month is : 30\n");
		}
		if (month == 7){
			printf ("The name of the month is : July\nThe number of days of the month is : 31\n");
		}
		if (month == 8){
			printf ("The name of the month is : August\nThe number of days of the month is : 31\n");
		}
		if (month == 9){
			printf ("The name of the month is : September\nThe number of days of the month is : 30\n");
		}
		if (month == 10){
			printf ("The name of the month is : October\nThe number of days of the month is : 31\n");
		}
		if (month == 11){
			printf ("The name of the month is : November\nThe number of days of the month is : 31\n");
		}
		if (month == 12){
			printf ("The name of the month is : December\nThe number of days of the month is : 31\n");
		}													
	}else{
		printf ("Invalid input!\nPlease try again\n------------------------------------------------------------------------------------------------\n\n");
		NameAndDaysOfTheMonth();
	}
	}
	
int MainMenu(){
	system("cls");
	int n,option = 0;
	printf ("*************************************************************\n");
	printf ("*          Date         : 10/08/2022                        *\n");
	printf ("*          Student Name : Phan Thanh Nam                    *\n");
	printf ("*          Student ID   : HE171690                          *\n");
	printf ("*************************************************************\n");
	printf ("*************************************************************\n");
	printf ("*     Please select the number for appropritate             *\n");
	printf ("*          [1]. Question 1                                  *\n");
	printf ("*          [2]. Question 2                                  *\n");
	printf ("*          [3]. Quit program                                *\n");
	printf ("*************************************************************\n");
		do {
         printf("Choose a number to run: ");
         scanf("%d",&option);
	     switch (option){
		     case 1:
		     	NameAndDaysOfTheMonth();
		     	break;
		     case 2:
	            printf ("*************************************************************\n");
	            printf ("* Please choose a number to start calculating!              *\n");
	            printf ("*          [1]. S = 1 + 2 + 3 + . . . + n                   *\n");
	            printf ("*          [2]. S = 1/1 + 1/3 + 1/5 + . . . + 1/n           *\n");
	            printf ("*          [3]. S = 1/2 + 1/4 + 1/6 + . . . + 1/n           *\n");
	            printf ("*          [0]. Return to main menu                         *\n");
	            printf ("*************************************************************\n");
                n = (int*) calloc(n, sizeof(int));
				printf("Enter n: ");
                scanf("%d",&n);
                fflush(stdin);	            
                do {
                    printf("\nChoose a number to run: ");
                    scanf("%d",&option);
	                switch (option){
	                 	case 1:
	                 		CalculatePlusToN();
	                 		break;
	                 	case 2:
	                 		CalculateOneDivideOdd();
	                 		break;
	                 	case 3:
	                 		CalculateOneDivideEven();
	                 		break;
	                 	case 0:
	                 		MainMenu();
	                 		break;
	                 	default:
	                 	    printf ("Your choice is incorrect\n");
							printf ("Please try again\n");
							break;
	                }
				}while (option !=0);
			    break;
		     case 3:
			     return 0;
		    default:
			     printf ("Your choice is incorrect\n");
			     printf ("Please try again\n");
			     break;
	}
	}while (option !=3);		
	}
			


int main (){
	int n,i,month;
	int option = 0;
	printf ("*************************************************************\n");
	printf ("*          Date         : 10/08/2022                        *\n");
	printf ("*          Student Name : Phan Thanh Nam                    *\n");
	printf ("*          Student ID   : HE171690                          *\n");
	printf ("*************************************************************\n");
	printf ("*************************************************************\n");
	printf ("*     Please select the number for appropritate             *\n");
	printf ("*          [1]. Question 1                                  *\n");
	printf ("*          [2]. Question 2                                  *\n");
	printf ("*          [3]. Quit program                                *\n");
	printf ("*************************************************************\n");
		do {
         printf("\nChoose a number to run: ");
         scanf("%d",&option);
	     switch (option){
		     case 1:
		     	NameAndDaysOfTheMonth();
		     	break;
		     case 2:
	            printf ("*************************************************************\n");
	            printf ("* Please choose a number to start calculating!              *\n");
	            printf ("*          [1]. S = 1 + 2 + 3 + . . . + n                   *\n");
	            printf ("*          [2]. S = 1/1 + 1/3 + 1/5 + . . . + 1/n           *\n");
	            printf ("*          [3]. S = 1/2 + 1/4 + 1/6 + . . . + 1/n           *\n");
	            printf ("*          [0]. Return to main menu                         *\n");
	            printf ("*************************************************************\n");
                n = (int*) calloc(n, sizeof(int));
				printf("Enter n: ");
                scanf("%d",&n);
                fflush(stdin);	            
                do {
                    printf("\nChoose a number to run: ");
                    scanf("%d",&option);
	                 switch (option){
	                 	case 1:
	                 		CalculatePlusToN();
	                 		break;
	                 	case 2:
	                 		CalculateOneDivideOdd();
	                 		break;
	                 	case 3:
	                 		CalculateOneDivideEven();
	                 		break;
	                 	case 0:
	                 		MainMenu();
	                 		break;
	                 	default:
	                 	    printf ("Your choice is incorrect\n");
							printf ("Please try again\n");
							break;
	                }
				}while (option !=0);
			    break;
		     case 3:
			     return 0;
		    default:
			     printf ("Your choice is incorrect\n");
			     printf ("Please try again\n");
			     break;
	}
	}while (option !=3);
	return 0;
}```

Xung đột quá kinh khủng bạn ạ. Đúng là bạn chưa hiểu gì về con trỏ, nhưng bài này thì bạn không cần dùng đến con trỏ làm gì.
Bạn chỉ cần thêm tham số cho hàm và lúc gọi hàm thì truyền giá trị cho nó thôi. Đáng lẽ Hàm và Tham số là thứ bạn đã học trước khi “đụng đến” con trỏ chứ nhỉ!?

4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?