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

[Help] for loop..

Can someone help me with my program. I have the code but there are some logical errors. The program should contain 100 numbers that have a menu, first the user should input a number then the menu will show: 1. it adds again a number, 2. it displays all the inputted numbers, 3. it will display the sum of all the numbers, 4. the program will exit. This is the code i had created i don't know I'm correct with the code in the case 2,3,4.

#include<stdio.h>
void main(void)
{
int sub,total=0,choice;
int numbers[100];
add:
do{
clrscr();
printf("Key-in numbers:\n\n");
for(sub=0;sub<100;sub++)
{
scanf("%d",&numbers[sub]);
printf("MENU");
printf("[1] Add Number.");
printf("[2] Display all numbers.");
printf("[3] Display Total.");
printf("[4] Exit.");
printf("Select your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
goto add;
break;
case 2:
printf("Number [%d]: %d",sub,numbers[sub]);
break;
case 3:
total=total+numbers[sub];
printf("\nTotal: %d.",total);
break;
case 4:
return 0:
default:
printf("Invalid Choice!");
getch();
goto add;
}
}
}while(choice!=3);
getch();
return 0;
}

saravinuya
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 0
 

First, format your code so we can follow it. That is necessary if you need others to read your code.

If you aren't sure if you are correct for 2, 3, & 4, try running the program. That will tell you whether you are correct or not.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

check this ,u need to use int main when u are using return 0; so try to go through gud tutorials ,rest google for the syntax or online comolplier..

#include<stdio.h>
int main()
{
int sub,total=0,choice;
int numbers[100];
add:
do{
printf("Key-in numbers:\n\n");
for(sub=0;sub<100;sub++)
{
scanf("%d",&numbers[sub]);
printf("MENU");
printf("[1] Add Number.");
printf("[2] Display all numbers.");
printf("[3] Display Total.");
printf("[4] Exit.");
printf("Select your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
goto add;
break;
case 2:
printf("Number [%d]: %d",sub,numbers[sub]);
break;
case 3:
total=total+numbers[sub];
printf("\nTotal: %d.",total);
break;
case 4:
default:
printf("Invalid Choice!");
return 0;
getch();
goto add;
}
}
}while(choice!=3);
getch();
return 0;
}
neeraj goswami
Newbie Poster
13 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

sorry im just a newbie here. when i select 2, which is to display all the numbers that were inputted, it only shows the last number that was inputted. When i select 3, which is to show the sum of all the inputted numbers, it doesn't add all the numbers that had been inputted. My code runs, but it has logical errors. Can someone help me?

saravinuya
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 0
 

Yes. Start by rereading my previous post -- first line..

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
#include<stdio.h>
void main(void)
{
int sub,total=0,choice;
int numbers[100];
add:
do{
clrscr();
printf("Key-in numbers:\n\n");
for(sub=0;sub<100;sub++)
{
scanf("%d",&numbers[sub]);
printf("MENU");
printf("[1] Add Number.");
printf("[2] Display all numbers.");
printf("[3] Display Total.");
printf("[4] Exit.");
printf("Select your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
goto add;
break;
case 2:
printf("Number [%d]: %d",sub,numbers[sub]);
break;
case 3:
total=total+numbers[sub];
printf("\nTotal: %d.",total);
break;
case 4:
return 0:
default:
printf("Invalid Choice!");
getch();
goto add;
}
}
}while(choice!=3);
getch();
return 0;
}
saravinuya
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 0
 
void main(void)


'main' should always return an 'int'(see here or here or here ,...the list will go "beyond infinity" ).
It's better not to use labels and goto statements as they make your program's control flow hard to follow
goto add;(see here ).Here in your case you can use 'continue' statement, it will do the same thing as you're doing with those 'goto' statements.

clrscr();
and

getch();

These are non-standard functions, so your code is better off without them.As you haven't included "conio.h" header, they will not work anyway.

diwakar wagle
Posting Whiz in Training
203 posts since Nov 2008
Reputation Points: 48
Solved Threads: 31
 

Take help from online compiler i didn't understand your code.

Mohammed Ammar
Light Poster
26 posts since Oct 2011
Reputation Points: 5
Solved Threads: 1
 

thanks for all the responses. it'll be a big help. :)

saravinuya
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 0
 

@saravinuya: well, did you really understand what WaltP tried to say about FORMATTING ?? Did you read the gidnetwork link she provided? ( click here , if you haven't)
So next time you post try to keep that in mind...because its very difficult for us to read un-formatted code. Hopefully, you'll understand.

NP-complete
Junior Poster
127 posts since May 2010
Reputation Points: 78
Solved Threads: 15
 


I'm not sure why you have a menu option to input data, yet at the same time, you're prompting for input with each iteration of the for loop. It seems like you'd need just the one loop.

int idx = 0, numbers[100] = { 0 };

do {
   // show menu
   // get choice
   switch(choice) {
      case 1 : // prompt user for input
               // store input (if valid) in numbers array and increment idx
               break;
      case 2 : // idx would contain all significant data entered thus far
               for(i=0; i < idx; ++i) { 
                  printf("%d ", numbers[i]);
               }
               break;
      case 3 : // basically the same as 2, loop through the valid data
               // sum the numbers array and output the result
               break;
      case 4 : // display exit message or such
               break;
      default : // ...
   }
} while(idx < 100 && choice != 4); // short circuit loop on idx being valid

I'd be leery of using scanf; especially with numbers. Maybe something like that.
HTH

foreshadowed
Newbie Poster
12 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

uhmm, it's ok i understand. sorry for my faults. :)

#include<stdio.h>
void main(void)
{
   int sub,total=0,choice;
   int numbers[100];
   add:
   do{
      clrscr();
      printf("Key-in numbers:\n\n");

      for(sub = 0 ; sub < 100 ; sub++)
     {
         scanf("%d",&numbers[sub]);
         printf("MENU");
         printf("[1] Add Number.");
         printf("[2] Display all numbers.");
         printf("[3] Display Total.");
         printf("[4] Exit.");
         printf("Select your choice: ");
         scanf("%d",&choice);

         switch(choice){
            case 1:
              goto add;
         break;
            case 2:
              printf("Number [%d]: %d",sub,numbers[sub]);
         break;
            case 3:
              total = total + numbers[sub];
              printf("\nTotal: %d.",total);
         break;
            case 4:
              return 0:
         default:
              printf("Invalid Choice!");
              getch();
              goto add;
         }
     }
}while(choice!=3);
getch();
return 0;
}
saravinuya
Newbie Poster
5 posts since Jul 2011
Reputation Points: 27
Solved Threads: 0
 
case 1 : 
      goto add;
   break;


this bit will basically reset everything but total. each time your user chooses "add a number," he or she will be prompted to enter a number starting at numbers[0] and prompted for a choice; by the looks of things, the same thing will occur for the default case.

case 2 :
      printf("Number: [%d] %d\n", sub, numbers[sub]);
   break;


this one will display whatever value happens to be stored in numbers[sub]; you won't get all of the values currently stored in your array. you'll need a loop inside of the case starting at 0 and going through sub - 1.

case 3 : 
      total = total + numbers[sub];
      printf("\nTotal: %d.", total);
   break;


this one may or may not have an accurate total. depending on what menu option the user chooses, total may be some arbitrary value of re-initialized array entires and not the direct sum of numbers[0-99].

I haven't tried your code, but I suspect the only way to get a meaningful total would be to choose menu option 2 each iteration of sub.

HTH

foreshadowed
Newbie Poster
12 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

seprate a for loop first.
1) which takes only numbers.

for(int i=0;i<100;i++)
{

}

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

total will not include all numbers because for 1 loop of sub you are calculating and displaying only 1 number.

so separate your loops.

1)
for(sub=0;sub<100;sub++)
{
scanf("%d",&number[sub]);
}

and then within case '3':

for(sub=0;sub

dev90
Light Poster
38 posts since Sep 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: