• 05-02-2002
Hi im writing a program for a project and dont know why i cant refer to the math1 funtion from inside the void option1(void) function(see below) any insight into this would be appreciated.

Code:

``` #include <stdio.h> #include <stdlib.h> char option, optiona; void math1(void)  {         system("cls");         printf("This program will calculate total, average and mean\nof a set of numbers you calculate.\n\n"); float x, sum, sq; int i, n;         printf("How many numbers do you wish to use? ");         scanf("%d", &n);         printf("Please enter number  1: ");         scanf("%f", &x);         sum = x;/*first value entered*/         sq = x*x; for (i = 2; i <= n; i++)/*i++ OR i = i + 1 OR i += 1*/ {         printf("Please enter number %2d: ", i);         scanf("%f", &x);         sum += x;         sq += x*x; }         printf("Sum = %10.3f, average = %10.3f, sum of squares = %10.3f\n", sum, sum / n, sq); }                 void option1(void) {                       system("cls");         printf("\t\t*****************\n");         printf("\t\t*Maths Functions*\n");         printf("\t\t*****************\n\n");                printf("Welcome to the maths functions programs.\n\n");         printf("Please select which option you want.\n\n");                printf("1. Program to find the total, average and mean of a set of numbers\n\n");                 scanf("%d", &optiona);                               if (optiona == '1')             math1();                else if (optiona <= 0 || optiona > 1)             printf("NO!");       } void option2(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 2, details later*\n");         printf("\t\t*************************\n\n"); } void option3(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 3, details later*\n");         printf("\t\t*************************\n\n"); } void option4(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 4, details later*\n");         printf("\t\t*************************\n\n"); } void option5(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 5, details later*\n");         printf("\t\t*************************\n\n"); } void option6(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 6, details later*\n");         printf("\t\t*************************\n\n"); } struct personal{         char name[30];         }prs_info; main() {         printf("\t\t*********************************************\n");         printf("\t\t*PROGRAMMING PROJECT, SEMESTER 2 (2001/2002)*\n");         printf("\t\t*********************************************\n\n");         printf("Hello, this is a data processing package,\n\n");         printf("Please enter your name so we can get started: ");         fgets(prs_info.name, 30, stdin);         do{         system("cls");         printf("Ok %sPlease choose from the following options (1 - 6)\n\n\n", prs_info.name);                   printf("\t1. Maths Functions Package.\n\n");         printf("\t2. File Editing Package.\n\n");         printf("\t3. Option3\n\n");         printf("\t4. Option4\n\n");         printf("\t5. Option5\n\n");         printf("\t6. Option6\n\n");         scanf("%c", &option); if (option =='1')   option1();    else if (option == '2')   option2(); else if (option == '3')   option3(); else if (option == '4')   option4(); else if (option == '5')   option5(); else if (option == '6')   option6(); } while (option <= '0' || option > '6'); }```
• 05-02-2002
Hammer
You're defining optiona as a char, then using %d within scanf to populate it.

Try using %c.
Or defining optiona as an int.

Either way, it (and maybe you!) appear to be getting confused ;)
• 05-02-2002
thanks hammer but that wasnt it, that was just me trying to change the declaration from int to char and left it like that accidently before i copied it to this thread, any other suggestions? :)
• 05-02-2002
Monster
Here's something I picked up from another thread. I think this will help.
Quote:

Originally posted by Prelude

Because scanf is lame. What happens is when you enter input, scanf will read what you tell it to, but if there is anything else in the stream that matches the next call to scanf, it will read that immediately, not promting the user for input. Such is the case with string input for scanf, usually the culprit is a newline character that scanf leaves in the input stream. The next call reads the newline and quits because scanf is delimited by a newline and whitespace when reading string data.

-Prelude

Maybe you can use the getch or getchar function instead.
• 05-02-2002
Hammer
Quote:

Originally posted by Monster
Here's something I picked up from another thread. I think this will help.

Maybe you can use the getch or getchar function instead.

Yep, that was get a lengthy conversation for such a *simple* topic if I remember rightly, maybe you should have a look at it Brad. Do a search, it was in the last day or two I think.
• 05-02-2002
i made it work by changing the choices on the maths list to A and not 1 (i dont know why this had an effect). Now the probelm seems to be that it is looping twice before i can type anything( it'd probably be best if you could run the program as it shows the problem obviously) and then the maths program doesnt do the final calculation. im sure its just 1 little thing but i cant see where. thanks

altered code from last time:

Code:

``` #include <stdio.h> #include <stdlib.h> char option, optiona; void math1(void)  {         system("cls");         printf("This program will calculate total, average and mean\nof a set of numbers you calculate.\n\n"); float x, sum, sq; int i, n;         printf("How many numbers do you wish to use? ");         scanf("%d", &n);         printf("Please enter number  1: ");         scanf("%f", &x);         sum = x;/*first value entered*/         sq = x*x; for (i = 2; i <= n; i++)/*i++ OR i = i + 1 OR i += 1*/ {         printf("Please enter number %2d: ", i);         scanf("%f", &x);         sum += x;         sq += x*x; } printf("Sum = %10.3f, average = %10.3f, sum of squares = %10.3f\n", sum, sum / n, sq); }                 void option1(void) {         do {                 //system("cls");         printf("\t\t*****************\n");         printf("\t\t*Maths Functions*\n");         printf("\t\t*****************\n\n");                printf("Welcome to the maths functions programs.\n\n");         printf("Please select which option you want.\n\n");                printf("A. Program to find the total, average and mean of a set of numbers\n\n");         optiona=getchar();               {       if (optiona == 'a' || optiona == 'A')           math1();              else if (optiona != 'a'|| optiona != 'A')       printf("Thats not an option!\n");       } } while (optiona != 'a'|| optiona != 'A');      } void option2(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 2, details later*\n");         printf("\t\t*************************\n\n"); } void option3(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 3, details later*\n");         printf("\t\t*************************\n\n"); } void option4(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 4, details later*\n");         printf("\t\t*************************\n\n"); } void option5(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 5, details later*\n");         printf("\t\t*************************\n\n"); } void option6(void) {         system("cls");         printf("\t\t*************************\n");         printf("\t\t*Option 6, details later*\n");         printf("\t\t*************************\n\n"); } struct personal{         char name[30];         }prs_info; main() {         printf("\t\t*********************************************\n");         printf("\t\t*PROGRAMMING PROJECT, SEMESTER 2 (2001/2002)*\n");         printf("\t\t*********************************************\n\n");         printf("Hello, this is a data processing package,\n\n");         printf("Please enter your name so we can get started: ");         fgets(prs_info.name, 30, stdin);               do{               system("cls");         printf("Ok %sPlease choose from the following options (1 - 6)\n\n\n", prs_info.name);                        printf("\t1. Maths Functions Package.\n\n");         printf("\t2. File Editing Package.\n\n");         printf("\t3. Option3\n\n");         printf("\t4. Option4\n\n");         printf("\t5. Option5\n\n");         printf("\t6. Option6\n\n");         option=getchar();         //scanf("%c", &option); if (option =='1')   option1();    else if (option == '2')   option2(); else if (option == '3')   option3(); else if (option == '4')   option4(); else if (option == '5')   option5(); else if (option == '6')   option6(); } while (option <= '0' || option > '6'); }```
• 05-02-2002
Prelude
Change this
Code:

```printf("A. Program to find the total, average and mean of a set of numbers\n\n"); optiona=getchar();```
to this
Code:

```printf("A. Program to find the total, average and mean of a set of numbers\n\n"); while ( getchar() != '\n' ); optiona=getchar();```
Once you clear the input stream the program will wait for user input instead of using what is in the stream.

-Prelude
• 05-02-2002
thanks prelude, ok, how can i return to the main function from functions above the main?

• 05-02-2002
Hammer
Quote:

how can i return to the main function from functions above the main?
When a function finishes it (normally) returns to it's caller (unless you do things like exit() etc).

I expect your problem is related to the fact your prog only gives the user the ability to work one option on the menu, then it terminates. You'll need to incorporate an Exit option to allow the user to say when they're done.

Sorry if I'm way off base with understanding what ya mean ;)
• 05-02-2002
yes i think you are on track with wot i mean hammer, after i type in the variables into the maths program and i have a result how do i return to the previous menu? Thanks