hi, my name is vic...i got one problem cannot fix....output for the number of times each of the tasks has been invoked...i run 3 times each of the tasks but the output always is this program has run 1 times.....

#include<stdio.h>
#include<stdlib.h>

void doTaskA(){
    printf("Start of Task A\n\n");
    int input_1, input_2, input_3;
    printf("Start of TASK A\n\n");
    printf("Please enter 3 integer.\n");
    scanf("%d\n%d\n%d",&input_1, &input_2, &input_3);
    if ((input_3>input_1) && (input_3>input_2))
        printf("/nThe biggest number is %d\n", input_3);
    else if ((input_2>input_1) && (input_2>input_3))
        printf("/nThe biggest number is %d\n", input_2);
    else 
        printf("/nThe biggest number is %d\n",input_1);

}



void doTaskB(){
int row, c, n, temp;

   printf("Start of Task B\n\n");
printf("Enter the number of rows in pyramid of stars you wish to see ");
   scanf("%d",&n);

   temp = n;

   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
         printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row - 1 ; c++ )
         printf("*");

      printf("\n");
   }

}



void doTaskC(){
 int n, c, k, space;
  printf("Start of Task c\n\n");

  scanf("%d", &n);

   space = 0;

   for ( k = n ; k >= 1 ; k-- )
   {
       for ( c = 1 ; c <= space ; c++ )
       printf(" ");

       space++;

       for ( c = 1 ; c <= k ; c++)
          printf("%d", c);

       printf("\n");
   }


}



void displayMenu(int &option) {



   printf("\nPlease choose one of the following :");
   printf("\n\n1. Task A\n");
   printf("2. Task B\n");
   printf("3. Task C\n");
   printf("4. Task D ( Exit This Program )\n");


   printf("\nPlease enter your selection :");
   scanf("%d",&option);

}

void howMany(){   

   static int count = 0;
   count++;


   printf("This program has run"  " %d "  "times\n\n\n",count);

   system("pause");

  }

 int main(){
    int option =0;
    bool exit = false;
    while(!exit) {
displayMenu(option);

        switch(option) {
            case 1:
                doTaskA();
                continue; 
            case 2:
                doTaskB();
                continue;
            case 3:
                doTaskC();
                continue;
            case 4:
                howMany();
                exit = true;
                break;
            default:
                printf("Incorrect selection. Try again.\n");
                continue;
}
}

    return 0;
}

Dani AI

Generated

Short answer: the counter is only being incremented when you call howMany(), so it will always print 1. runs tasks several times but never increments the counter while the program is running — the count in howMany() is initialized and only bumped on program exit. was on the right track: increment the counter where selections/tasks happen, not only when printing the result.

Practical fixes (pick one):

  • Count total menu selections: keep a file-scope counter and increment it each time the menu is shown (or each time a choice is made). Print that counter from howMany() when exiting.
  • Count per task: keep three counters (one per task) and increment inside doTaskA(), doTaskB(), and doTaskC() so you can report how many times each task ran.

Example (minimal, use different names than in your code):

int totalSelections = 0;   // file scope

// inside displayMenu or just after a valid selection:
totalSelections++;

// on exit:
printf("This program has run %d times\n", totalSelections);

Other bugs to fix while you’re here:

  • Wrong newline sequences: use \n (backslash n). Your code prints "/n..." which literally prints /n.
  • Don’t put \n characters inside scanf formats (e.g. scanf("%d\n%d\n%d", ...)) — that can make scanf wait unexpectedly. Use scanf("%d %d %d", &a, &b, &c) and check the return value.
  • Prompt before each scanf so the user knows what to enter.
  • system("pause") is Windows-only; prefer a portable prompt (like reading a final newline) or remove it.
  • Compile with warnings (g++ -Wall -Wextra) and fix each warning — they often point to logic mistakes.

Quick debug tip: add a temporary printf that prints your counters right after you increment them to verify behavior during the run.

Recommended Answers

All 2 Replies

#include<stdio.h>
#include<stdlib.h>

void doTaskA(){
	printf("Start of Task A\n\n");
	int input_1, input_2, input_3;
	printf("Start of TASK A\n\n");
	printf("Please enter 3 integer.\n");
	scanf("%d\n%d\n%d",&input_1, &input_2, &input_3);
	if ((input_3>input_1) && (input_3>input_2))
		printf("/nThe biggest number is %d\n", input_3);
	else if ((input_2>input_1) && (input_2>input_3))
		printf("/nThe biggest number is %d\n", input_2);
	else 
		printf("/nThe biggest number is %d\n",input_1);

 }



void doTaskB(){
	int row, c, n, temp;

	printf("Start of Task B\n\n");
	printf("Enter the number of rows in pyramid of stars you wish to see ");
	scanf("%d",&n);

	temp = n;

	for ( row = 1 ; row <= n ; row++ )  {	
		for ( c = 1 ; c < temp ; c++ )
			printf(" ");

		temp--;

		for ( c = 1 ; c <= 2*row - 1 ; c++ )
			printf("*");

		printf("\n");
	 }

 }



void doTaskC()  {
	int n, c, k, space;
	printf("Start of Task c\n\n");

	scanf("%d", &n);

	space = 0;

	for ( k = n ; k >= 1 ; k-- ) {
		for ( c = 1 ; c <= space ; c++ )
			printf(" ");

		space++;

		for ( c = 1 ; c <= k ; c++)
			printf("%d", c);

		printf("\n");
	}
 }


static int count = 0; 

void displayMenu(int &option) {
	count++;

	printf("\nPlease choose one of the following :");
	printf("\n\n1. Task A\n");
	printf("2. Task B\n");
	printf("3. Task C\n");
	printf("4. Task D ( Exit This Program )\n");


	printf("\nPlease enter your selection :");
	scanf("%d",&option);
 }

void howMany()  { 
//	static int count = 0;
//	count++;


	printf("This program has run" " %d " "times\n\n\n",count);

	system("pause");
 }

int main()  {
	int option =0;
	bool exit = false;
	while(!exit) {
		displayMenu(option);

		switch(option) {
			case 1:
				doTaskA();
				continue; 
			case 2:
				doTaskB();
				continue;
			case 3:
				doTaskC();
				continue;
			case 4:
				howMany();
				exit = true;
				break;
			default:
				printf("Incorrect selection. Try again.\n");
				continue;
		 }
 	 }
	return 0;
}

You should make the 'count' variable get incremented while calling the 'displayMenu' function if that's what you want. I'm not sure though what is it that you require.

what is the error???/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.