go0gle and the letter C

21 replies [Last post]
Joined: 01/07/2010

as you know I started to programming in C and I said:Why not Share basic programs and the users could give me tips?
And my brain said "Ok".
And for that is this space.

WARNING!!!!!
I don´t want superb comments,
I just want some tips and observations.

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 01/07/2010

switch exercise

a very simple one.

#include <stdio.h>
 
 
int main (void)
{
    char scaneo;
 
    printf("Introduce la letra:\n");
 
    scanf("%c", &scaneo);
 
 
    switch (scaneo){
       case 'a':
            printf("%c es vocal\n", scaneo); 
            break;
 
 
            case 'e':
            printf("%c es vocal\n", scaneo); 
            break;
 
               case 'i':
            printf("%c es vocal\n", scaneo); 
            break;
 
 
 
            case 'o':
            printf("%c es vocal\n", scaneo); 
            break;
 
            case 'u':
            printf("%c es vocal\n", scaneo); 
            break;
 
            default:
                    printf("%c  no es  vocal! \n",scaneo);
                    break;
 
           }
 
    getch();
}

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 08/06/2010

You should return 0 at the end.

Another Planet finally has an official release! Download chapters 1 through 3 here! Thank you for waiting so long while I kept starting over.

Joined: 01/07/2010

I have a question about that, when I "return 0;" the program inmediatly finish and don´t give me time to analize the result, is this normal??

if this is normal I will keep using getch,for analizing results

also putting "Getch" at the end is a bad practice??

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 09/01/2009

Generally, you should be running command-line programs from the command line, so you can see all the stuff you're printf()'ing. Then, you won't have to getch() at the end.

Oh, yeah, and getch() is compiler-specific, since it's not a C standard. It'd probably be a good idea to not use it, yes.

And return 0; just ends the function and tells the operating system how the program went. Generally, the OS disregards this entirely, and seems to me that your compiler is automatically tacking on a return 0 at the end anyway, but it's a good idea to put the return 0 at the end so you don't confuse yourself. It's int main() after all, not void main().

And if you want, you can still getch() right before the return 0;. The "return 0;" line is where the function exits. Anything after it will be skipped, yes.

Joined: 01/07/2010

MOM4Evr wrote:

Oh, yeah, and getch() is compiler-specific, since it's not a C standard. It'd probably be a good idea to not use it, yes.

And if you want, you can still getch() right before the return 0;. The "return 0;" line is where the function exits. Anything after it will be skipped, yes.


That´s all I need.Thanks MOM,(Tagged as usefull).

by the way i will post for tomorrow a program I made recently with simple functions that make conversions and a formula for distance,speed and time.

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 12/23/2010

I made a small program once in C++ that converts Celsius to Fahrenheit and vice versa. I don't know where the code is, and I probably can't find it anymore, but I recommend to give it a go for good practice.

Joined: 01/07/2010

Program that get the speed,time and distance.And also Convert distance time and speed units.By choice and saving results

Took 6 hours

#include <stdio.h>
#include <stdlib.h>
 
 
 
int main ()
{
    float velocidad, tiempo, distancia,unidadd=0,unidadn=0, unidadt=0,unidadj=0,unidade=0;
 
      int conversion,opcion,repeticion;
 
    printf("necesitas  convertir unidades? \n" );
    printf("5=si\n");
    printf("6=no\n");
    scanf("%d", &repeticion);
    while (repeticion !=6)
    {system("cls");
 
     printf("1=Convertir kilometros a metros \n");
      printf("1=Convertir centimetros a metros \n");
      printf("3=convertir minutos a segundos\n");
     printf("4=convertir horas a segundos \n");
     printf("5=convertir km\\h a m\\s \n ");
     printf("\n");
     printf("6=borrar todos los datos almacenados \n");
 
    scanf ("%d" , &conversion);
 
 
 
    switch(conversion){
    case 1:
          printf("Cuantos Kilometros ?   \n");
          scanf ("%f", &unidadd);
          unidadd=unidadd*1000;
          printf("Conversion: %.2f \n", unidadd);
          printf("los datos seran almacenados, presiona cualquier tecla para continuar\n"); 
         printf("\n");
    printf("Presiona 5 si quieres convertir mas unidades\n");
    printf("Presiona  6 para continuar\n");
 
    getch();
           break;
 
 
    case 2:
          printf("Cuantos centimetros ?   \n");
          scanf ("%f", &unidadn);
          unidadn=unidadn/100;
          printf("Conversion: %.2f \n", unidadn);
          printf("los datos seran almacenados, presiona cualquier tecla para continuar\n"); 
         printf("\n");
    printf("Presiona 5 si quieres convertir mas unidades\n");
    printf("Presiona  6 para continuar\n"); 
    getch();
           break;
 
 
    case 3:
          printf("Cuantos minutos ?   \n");
          scanf ("%f", &unidadt);
          unidadt=unidadt*60;
          printf("Conversion: %.2f \n", unidadt);
       printf("los datos seran almacenados, presiona cualquier tecla para continuar\n"); 
          printf("\n");
    printf("Presiona 5 si quieres convertir mas unidades\n");
    printf("Presiona  6 para continuar\n");
    getch();
           break;
 
 
 
    case 4:
          printf("Cuantas horas ?   \n");
          scanf ("%f", &unidadj);
          unidadj=unidadj*360;
          printf("Conversion: %.2f \n", unidadj);
          printf("los datos seran almacenados, presiona cualquier tecla para continuar\n"); 
         printf("\n");
    printf("Presiona 5 si quieres convertir mas unidades\n");
    printf("Presiona  6 para continuar\n");
    getch();
           break;
 
 
    case 5:
          printf("Cuantos Kilometros por hora ?   \n");
          scanf ("%f", &unidade);
          unidade=(unidade*1000)/3600;
          printf("Conversion: %.2f \n", unidade);
          printf("los datos seran almacenados, presiona cualquier tecla para continuar\n"); 
          printf("\n");
    printf("Presiona 5 si quieres convertir mas unidades\n");
    printf("Presiona  6 para continuar\n");
    getch();
           break;
 
 
      case 6:
           unidadd=0;
           unidadn=0;
            unidadt=0;
            unidadj=0;
            unidade =0;    
            system("cls");
           printf("Todos los datos almacenados han sido borrados\a \n");
           printf("\n");
            printf("Presiona 5 si quieres convertir mas unidades\n");
    printf("Presiona  6 para continuar\n");
    getch();
    break;
 
 
 
    default:getch();
     break;}
 
    repeticion=0;
 
    scanf ("%d", &repeticion);
 
}
 
   system("cls");
    if((unidadd + unidadn+unidadt+ unidadj+ unidade)!=0)
  { printf("resultados almacenados\n");
     printf("\n");}
   if(unidadd >0)
  { printf("metros :%.2f\n", unidadd);}
   if(unidadn >0)
  { printf("metros :%.2f\n", unidadn);}
   if(unidadt >0)
  { printf("segundos :%.2f\n", unidadt);}
   if(unidadj >0)
  { printf("segundos :%.2f\n", unidadj);}
   if(unidade >0)
  { printf("metros\\segundos :%.2f\n", unidade);}
     printf("\n");
    printf("Que quieres sacar? \a \n");
     printf("1=velocidad \n");
      printf("2=tiempo\n");
       printf("3=distancia\n");
    scanf("%d", &opcion);
 
    switch (opcion)
    {case 1 : 
          printf("Introduce distancia: \n");
          scanf ("%f", &distancia);
          printf("Introduce tiempo: \n");
          scanf ("%f", &tiempo);
 
 
          velocidad= distancia/tiempo;
 
          printf ("velocidad es igual a %.2f  m\\s \n", velocidad);
          break;
 
 case 2 : 
      printf("Introduce distancia: \n");
          scanf ("%f", &distancia);
          printf("Introduce velocidad: \n");
                   scanf ("%f", &velocidad);
 
 
          tiempo= distancia/velocidad;
 
          printf ("tiempo es igual a %.2f \a \n", tiempo);
          break;
 
  case 3 : 
       printf("Introduce velocidad: \n");
          scanf ("%f", &velocidad);
          printf("Introduce tiempo: \n");
          scanf ("%f", &tiempo);
 
 
          distancia= tiempo*velocidad;
 
          printf ("distancia es igual a %.2f \a \n", distancia);        
          break;
 
   default:
           printf("Eso no es logico!");      
           break;
           }
 
           getch();
 
 
    return 0;
 
 
    }

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 09/01/2009

Si. I mean, whatever Spanish for "cool" is.

Just a few pointers and questions:
-Still using getch(), I see. Is it because you're not running the program from a command line, or what?
-What IDE are you using? Visual Studio? Code::Blocks? Borland (I thought that getch() was Borland-specific, but I could be mistaken. That was in 1993, after all)? getch() doesn't work in GCC, just so you know. You can replace it safely with getchar(); you'll just have to hit Enter after typing.
-Commenting your code is always a good idea, even if it's a small, simple program. The more comments in a program, the better. Start getting in a habit of at least commenting every block (Like every switch case statement or something). In complex code that I write, I comment practically every line. And I never regret it.
-By the way, "system("cls");" doesn't work in Linux. Tongue Stinking DOS/Unix command differences...

But overall, very nice. Smile And a good amount of code, too. I can tell you're doing more and more complex stuff here (Though admittedly my Spanish is a bit rusty, so I can't tell exactly what you're doing 100% of the time).

Oh, and for the record, always watch for division by 0 when writing a program:

Que quieres sacar?  
1=velocidad 
2=tiempo
3=distancia
1
Introduce distancia: 
45
Introduce tiempo: 
0
velocidad es igual a inf  m\s 

Didn't crash here, but in a lot of cases it will.

EDIT: Oooh, ooh, and add a feature to convert cubic light years into cubic angstroms. My friend did that once; it was fun. Smile

Joined: 08/06/2010

Use this:

#ifdef _WIN32
system("cls");
#else
cout << "\33c";
#endif

It clears the screen on ANY operating system.

Another Planet finally has an official release! Download chapters 1 through 3 here! Thank you for waiting so long while I kept starting over.

Joined: 01/07/2010

0_0 Third time i think goofans community help me very massive!

I didn´t got the "0" value as a option, I will be in that , and also i´m using
"Dev C++"IDE.
I´m contempling to do more global functions for a better run in other ambientations like linux.
Thanks!
Tomorrow New one!

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 09/01/2009

The proper term is "Operating System", not "ambientation". Tongue

Hmm, strange that Dev-C++ allows that, since it uses GCC by default. Did you configure it to use some other compiler than GCC? If you dunno, that's fine.

By the way, development is halted on Dev-C++. Code::Blocks is a lot like it, only more up to date. Just so you know. Smile

Anyhow, happy programming! Remember that the best language is LOVE.
-MOM

Joined: 01/07/2010

MOM4Evr wrote:
The proper term is "Operating System", not "ambientation".

It was a metaphor.

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 09/01/2009

G0o-gle wrote:
It was a metaphor.

The metaphor would be better if it was a real word. Tongue

Joined: 12/23/2010

Sure it is. Here's WordWeb's definition:

WordWeb wrote:

Word not found: ambientation

Look on the web

Joined: 08/06/2010

I personally use the MinGW compiler, which is just like GCC for Windows except it doesn't put any restrictions on what you can compile with it.

Another Planet finally has an official release! Download chapters 1 through 3 here! Thank you for waiting so long while I kept starting over.

Joined: 07/08/2011

I use Code::Blocks which compiles via MinGW. It has options in the Project Properties to specify what platforms to allow building for.

Joined: 01/07/2010

Ok, perfect people:
Ambience!

Good?
I will search for Code Blocks.

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO

Joined: 09/01/2009

Albino Pokey wrote:
except it doesn't put any restrictions on what you can compile with it.

And regular GCC does? I'm confused. I thought that GCC and MinGW were essentially identical.

@G0o-gle: Still no clue what you're talking about. How is an operating system ambient?
And yah, Code::Blocks is good. Just make sure that you download the version with MinGW included. Otherwise you won't be able to compile anything (Unless you have MinGW already installed, which come to think of it, you might since I think Dev-C++ comes with MinGW by default. I guess just try the regular download and if it doesn't work, download the MinGW version).

Joined: 08/06/2010

Some versions of GCC say in the license that anything you compile needs to be LGPL. Others make it so that you need to have a GCC DLL on your computer. The cygwin one does that, for example: if you install Cygwin without GCC, you can't run a Cygwin-GCC-compiled EXE.

Another Planet finally has an official release! Download chapters 1 through 3 here! Thank you for waiting so long while I kept starting over.

Joined: 09/01/2009

Hmm, interesting. Learn something new every day. Smile

Joined: 01/07/2010

MOM4Evr wrote:

@G0o-gle: Still no clue what you're talking about. How is an operating system ambient?


Forget it.

My attempt to make a demonstration of functions failed.I will try it... one more time!

Hey You!
Start the fun and Install the fan made CHAPTER 6: MOON OF GOO