Thread: Need some help with my C code

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    3

    Need some help with my C code

    Just started programming with C and now im stuck. Sorry if this code looks totally moronic, it's my first one. Okay, so I made this simple program, which asks your name and age first, asks a random imput to continue. Then a menu should appear which asks you to choose from 4 options. 1 option is to display information about this programs objective. 2 option does the objective with imput numbers. 3rd option should shoud shut down the program (but i keep getting an error because of it). Also, it displays a synthax error of "return 0". Could anyone help a beginner? Because my head is not clear after doing this for a whole day.

    This is how my code looks like:
    Code:
    #include<stdio.h>
    #include<locale.h>
    #include<string.h>
    #include<conio.h>
    int main()
    {
     setlocale(LC_ALL, "Lithuanian");
     int a;
     float t1, t2, k, r;
     char v[20];
     printf("Įveskite savo vardą: ");
     scanf("%s", v);
     printf("Įveskite savo amžių: ");
     scanf("%d", &a); 
     printf("Vardas: %s \n", v);
     printf("Amžius: %d \n", a);
     void menu();
      {
         printf("--------------------------------------------\n");
         printf("            Pagrindinis meniu: \n"); /*Menu*/
         printf("--------------------------------------------\n");
         printf("        1. Programos informacija; \n");
         printf("        2. Programos vygdymas; \n");
         printf("        3. Programos pabaiga. \n"); /*End of the program*/
         printf("--------------------------------------------\n");
         printf(" Pasirinkite skaičių iš pagrindinio meniu...\n");
         printf("--------------------------------------------\n");
      }
         main();
             char choice;
             do 
             {
                 menu();
                 scanf("%c", &choice);
                 switch(choice)
                 {
                    case '1': printf("Įvedami du krovininio traukinio važiavimo greičiai tarpstočiuose dešimtainėje skaičiavimo sistemoje, \n");
                              printf("kuriuos programa padaugina vieną iš kito, sandaugos rezultatą padaugina iš krovininio traukinio masės \n");
                              printf("(apibrėžtos konstantos), o galutinį rezultatą atspausdina šešioliktainėje skaičių sistemoje. \n");
                              break;
                    case '2': printf("Įveskite pirmo traukinio greitį: ");
                              scanf("%f",t1);
                              printf("Įveskite antro traukinio greitį: ");
                              scanf("%f",t2);
                              printf("Įveskite traukinio svorį: ");
                              scanf("%f",k);
                              r = t1 * t2 * k;
                              printf("Atsakymas yra %f",k);
                    case '3': exit();
                    default:  printf("Klaidingas skaičius."); /*wrong number*/ 
                 }
    			 while (choice <=  3 || choice >= 1);
             }
     return 0;
    }



    And these are the errors i get:


    Code:
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(12): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(14): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(34): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(42): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(44): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(46): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(49): error C3861: 'exit': identifier not found
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(52): error C2059: syntax error : '&&'
    1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp(54): error C2059: syntax error : 'return'

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Here are a few changes.
    Code:
    /*!! this #define disables all those  */
    /*!! warning C4996: 'scanf': This function or variable may be unsafe */
    #define _CRT_SECURE_NO_WARNINGS
    
    #include<stdio.h>
    #include<locale.h>
    #include<string.h>
    #include<stdlib.h>  /*!! needed for exit */
    
    /*!! this header file is obsolete - don't use it */
    /*!! #include<conio.h> */
    
    /*!! moved from within main */
    /*!! C doesn't allow nested functions */
    void menu()
      {
         printf("--------------------------------------------\n");
         printf("            Pagrindinis meniu: \n"); /*Menu*/
         printf("--------------------------------------------\n");
         printf("        1. Programos informacija; \n");
         printf("        2. Programos vygdymas; \n");
         printf("        3. Programos pabaiga. \n"); /*End of the program*/
         printf("--------------------------------------------\n");
         printf(" Pasirinkite skaičių iš pagrindinio meniu...\n");
         printf("--------------------------------------------\n");
      }
      
    int main()
    {
     int a;
     float t1, t2, k, r;
     char v[20];
     /*!! moved call to be compatible with C */
     setlocale(LC_ALL, "Lithuanian");
     printf("Įveskite savo vardą: ");
     scanf("%s", v);
     printf("Įveskite savo amžių: ");
     scanf("%d", &a); 
     printf("Vardas: %s \n", v);
     printf("Amžius: %d \n", a);
             char choice;
             do 
             {
                 menu();
                 scanf("%c", &choice);
                 switch(choice)
                 {
                    case '1': printf("Įvedami du krovininio traukinio važiavimo greičiai tarpstočiuose dešimtainėje skaičiavimo sistemoje, \n");
                              printf("kuriuos programa padaugina vieną iš kito, sandaugos rezultatą padaugina iš krovininio traukinio masės \n");
                              printf("(apibrėžtos konstantos), o galutinį rezultatą atspausdina šešioliktainėje skaičių sistemoje. \n");
                              break;
                    case '2': printf("Įveskite pirmo traukinio greitį: ");
                              scanf("%f",&t1);/*!! watch your scanf calls; make sure & is used correctly */
                              printf("Įveskite antro traukinio greitį: ");
                              scanf("%f",&t2);/*!! watch your scanf calls; make sure & is used correctly */
                              printf("Įveskite traukinio svorį: ");
                              scanf("%f",&k);/*!! watch your scanf calls; make sure & is used correctly */
                              r = t1 * t2 * k;
                              printf("Atsakymas yra %f",k);
                    case '3': exit(0);/*!! exit needs a parameter value */
                    default:  printf("Klaidingas skaičius."); /*wrong number*/ 
                 }
              /*!! while moved to the matching closing brace */
             } while (choice <=  3 || choice >= 1);
     return 0;
    }


    > 1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp
    If you're writing C, then you need to make sure your source files are .c files, not .cpp files.
    The C standard that microsoft visual studio supports doesn't allow mixed declarations and statements - for example, your call to setlocale was before the variables.
    C++ allows this, as do later standards of C - but your C compiler doesn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    Quote Originally Posted by Salem View Post
    Here are a few changes.
    Code:
    /*!! this #define disables all those  */
    /*!! warning C4996: 'scanf': This function or variable may be unsafe */
    #define _CRT_SECURE_NO_WARNINGS
    
    #include<stdio.h>
    #include<locale.h>
    #include<string.h>
    #include<stdlib.h>  /*!! needed for exit */
    
    /*!! this header file is obsolete - don't use it */
    /*!! #include<conio.h> */
    
    /*!! moved from within main */
    /*!! C doesn't allow nested functions */
    void menu()
      {
         printf("--------------------------------------------\n");
         printf("            Pagrindinis meniu: \n"); /*Menu*/
         printf("--------------------------------------------\n");
         printf("        1. Programos informacija; \n");
         printf("        2. Programos vygdymas; \n");
         printf("        3. Programos pabaiga. \n"); /*End of the program*/
         printf("--------------------------------------------\n");
         printf(" Pasirinkite skaičių iš pagrindinio meniu...\n");
         printf("--------------------------------------------\n");
      }
      
    int main()
    {
     int a;
     float t1, t2, k, r;
     char v[20];
     /*!! moved call to be compatible with C */
     setlocale(LC_ALL, "Lithuanian");
     printf("Įveskite savo vardą: ");
     scanf("%s", v);
     printf("Įveskite savo amžių: ");
     scanf("%d", &a); 
     printf("Vardas: %s \n", v);
     printf("Amžius: %d \n", a);
             char choice;
             do 
             {
                 menu();
                 scanf("%c", &choice);
                 switch(choice)
                 {
                    case '1': printf("Įvedami du krovininio traukinio važiavimo greičiai tarpstočiuose dešimtainėje skaičiavimo sistemoje, \n");
                              printf("kuriuos programa padaugina vieną iš kito, sandaugos rezultatą padaugina iš krovininio traukinio masės \n");
                              printf("(apibrėžtos konstantos), o galutinį rezultatą atspausdina šešioliktainėje skaičių sistemoje. \n");
                              break;
                    case '2': printf("Įveskite pirmo traukinio greitį: ");
                              scanf("%f",&t1);/*!! watch your scanf calls; make sure & is used correctly */
                              printf("Įveskite antro traukinio greitį: ");
                              scanf("%f",&t2);/*!! watch your scanf calls; make sure & is used correctly */
                              printf("Įveskite traukinio svorį: ");
                              scanf("%f",&k);/*!! watch your scanf calls; make sure & is used correctly */
                              r = t1 * t2 * k;
                              printf("Atsakymas yra %f",k);
                    case '3': exit(0);/*!! exit needs a parameter value */
                    default:  printf("Klaidingas skaičius."); /*wrong number*/ 
                 }
              /*!! while moved to the matching closing brace */
             } while (choice <=  3 || choice >= 1);
     return 0;
    }


    > 1>c:\users\silvis\documents\visual studio 2010\projects\projektas\projektas\projektas.cpp
    If you're writing C, then you need to make sure your source files are .c files, not .cpp files.
    The C standard that microsoft visual studio supports doesn't allow mixed declarations and statements - for example, your call to setlocale was before the variables.
    C++ allows this, as do later standards of C - but your C compiler doesn't.
    Thanks for your reply. The reason i use C++ in C because my teacher told me to do so.

    About the code: Problem is, that when i use an input ENTER, i get two menus, immediately and one saying "wrong input" (in my language). Its fine that i get that two times after imputting the wrong number, but its not good that i get it twice right after i type my name and age. Also, 2nd case doesnt work, i dont really know why. Yes, i looked into why it didnt work, & wasnt the case. Judging by an error i get, & must be present. Anyway I'll try to do whatever I can to fix it. Thanks for the help again

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Thanks for your reply. The reason i use C++ in C because my teacher told me to do so.
    Perhaps you need a better teacher.
    Because if you learn a bunch of lazy habits because the C++ compiler is more forgiving about certain things, then you're in for a world of pain when you come across a real C compiler.

    > About the code: Problem is, that when i use an input ENTER, i get two menus
    Change this.
    scanf(" %c", &choice); /* note: leading space in format string */
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Silvis View Post
    Also, 2nd case doesnt work, i dont really know why. Yes, i looked into why it didnt work, & wasnt the case. Judging by an error i get, & must be present. Anyway I'll try to do whatever I can to fix it. Thanks for the help again
    The second case have no break at end. This means the execution fall through to case 3 and there is an exit. So at the end of case 2 the program will end.
    I write switch cases in the following style:
    Code:
    …
                switch(choice)
                {
                    case '1':
                        printf("Įvedami du krovininio traukinio važiavimo greičiai tarpstočiuose dešimtainėje skaičiavimo sistemoje, \n");
                        printf("kuriuos programa padaugina vieną iš kito, sandaugos rezultatą padaugina iš krovininio traukinio masės \n");
                        printf("(apibrėžtos konstantos), o galutinį rezultatą atspausdina šešioliktainėje skaičių sistemoje. \n");
                        break;
    
    
                    case '2':
                        printf("Įveskite pirmo traukinio greitį: ");
                        scanf("%f",t1);
                        printf("Įveskite antro traukinio greitį: ");
                        scanf("%f",t2);
                        printf("Įveskite traukinio svorį: ");
                        scanf("%f",k);
                        r = t1 * t2 * k;
                        printf("Atsakymas yra %f",k);
                        break;
    
    
                    case '3':
                        break;
    
    
                    default:
                        printf("Klaidingas skaičius."); /*wrong number*/
                }
    …
    You can see that every case have an empty line in front. You can clearly see where a case starts and if the previous case have a break.
    And as far as i see, your while condition is wrong.
    I think this is what you mean:
    Code:
    …
                while (choice <=  3 && choice >= 1); /* && (and) insteed of || (or) */
    or, i personally would write:
    Code:
    …
                while (choice > 0 && choice < 4);
    Last edited by WoodSTokk; 11-16-2016 at 07:28 AM.
    Other have classes, we are class

  6. #6
    Registered User
    Join Date
    Nov 2016
    Posts
    3
    Thank you everyone for trying to help me out with this problem. Salem, thank you very much, no more problems with menus anymore. Also, thanks alot woodstokk, my program doesnt end after using a 2nd case of my menu once (now i can do the action as much as I want. The only problem thats left is a random code of letters and numbers as an answer. Number formats and some int's were changed in my program, before any of your replies, i tried to rewrite it from scratch with every advice on this thread. So here is my current case 2 which DOES work but something is wrong with the formula.

    Beginning of the switch script:

    Code:
    		int const k = 5000;
    		char meniu;
    		int t1, t2, r;
    		do
    		{
    			menu();
                scanf(" %c", &meniu);
                switch(meniu)

    Current second case with every fix from this thread already applied:

    Code:
                    case '2': printf("Įveskite pirmo traukinio greitį: ");
                              scanf("%d",&t1);
                              printf("Įveskite antro traukinio greitį: ");
                              scanf("%d",&t2);
                              r = t1*t2*k;
                              printf("Atsakymas yra %x.",r);
    						  break;
    So okay, basically I have one constant number - an average mass of a train . Then i ask the user to type down speed of a first train and then speed of a second train. The program should multiply both of these and then multiply it with the given constant train mass number (5000). Each given number format is the one that is typed in requirements to be used. Logically thinking this equation should be correct. But as its my first program, im not really sure how this shouldnt work in C. This thing is keeping me from a finished program.

    Yeah and about the teacher. We dont really learn programming its our first and the last homework.

    p.s. sorry for horrible english

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM

Tags for this Thread