954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[Hijack Split] Urgent.......How to get my program to count how many times its ran????

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;
}
vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

You need to use an array of ints, one for each task. Or if you know there will be only 4 tasks then you can use 4 different int counters

int count1 = 0, count2 = 0, count3= 0;
int main(){
int option =0;
bool exit = false;
while(!exit) {
displayMenu(option);

switch(option) {
case 1:
doTaskA();
count1++;
continue; 
case 2:
doTaskB();
count2++;
continue;
case 3:
doTaskC();
count3++;
continue;
case 4:
howMany(count1,count2,count3);

exit = true;
break;
default:
printf("Incorrect selection. Try again.\n");
continue;
}
}

return 0;
}
[
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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 #include

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; }

You made Duplicate posts at the C and C++ forums and I received 3 private messages from you with the very same topic
You could have just made a single thread and waited patiently for a response from one our members

@AD
there's a [ at the end of your code

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 

i try your code already, but got error......

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
i try your code already, but got error......

I'm not a mind reader so you need to tell us what the error was and post the code.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
I'm not a mind reader so you need to tell us what the error was and post the code.

Error 5 error C2660: 'howMany' : function does not take 3 arguments

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
Error 5 error C2660: 'howMany' : function does not take 3 arguments



6 IntelliSense: too many arguments in function call

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

create the necessary arguments at your howMany function for it to work

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
create the necessary arguments at your howMany function for it to work


can u give me the example ???

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
void howMany(int count1, int count2, int count3 )



then print their values in the function

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
void howMany(int count1, int count2, int count3 )
then print their values in the function

when i run 3 times in my program, but output still zero......

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Post your newest code.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Post your newest code.
#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(int &count1, int &count2, int &count3)  { 
	int count = 0;
	count ++;

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

	system("pause");
 }


int main(){
int count1 = 0, count2 = 0, count3= 0;
int option =0;
bool exit = false;
while(!exit) {
displayMenu(option);

switch(option) {
case 1:
doTaskA();
count1++;
continue; 
case 2:
doTaskB();
count2++;
continue;
case 3:
doTaskC();
count3++;
continue;
case 4:
howMany(count1,count2,count3);

exit = true;
break;
default:
printf("Incorrect selection. Try again.\n");
continue;
}
}

return 0;
}
vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

You have to change HowMany() so that it displays the values of the 3 parameters. And those parameters don't have to be passed by reference. void howMany(int count1, int count2, int count3)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
You have to change HowMany() so that it displays the values of the 3 parameters. And those parameters don't have to be passed by reference. void howMany(int count1, int count2, int count3)

i change already....still same the result.......

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
You have to change HowMany() so that it displays the values of the 3 parameters. And those parameters don't have to be passed by reference. void howMany(int count1, int count2, int count3)

can u modify again the code ?.....because tomorrow i need to pass up this code already....

vic s
Newbie Poster
12 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

If it is that urgent, this can be done quickly and easily using the dreaded global variable. Not to recommend extensive use of globals but just this once they could make for an easy solution:

int f1,f2;
void functionOne(/*functionOne arguments*/)
{
    f1++;//increment f1
    //code for function One
}
void functionTwo(/*functionTwo arguments*/)
{
    f2++;//increment f2
    //code for function Two
}
void showCounts()
{
    cout<<"functionOne: "<<f1<<" times.\nfunctionTwo: "<<f2<<" times.";
}
int main()
{
    showCounts();
    functionOne();
    showCounts();
    functionTwo();
    showCounts();
    return 0;
}


That is just an example of course, but you can use this principle in your code if you need to.

Labdabeta
Posting Pro in Training
489 posts since Feb 2011
Reputation Points: 27
Solved Threads: 18
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You