Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C Program Error


ghost's Avatar
0 0

Ok, well every time i run this and input a number i get the "This program has encountered an error and needs to close." It's pretty obvious as to what I'm trying to do so…Here's the code:

int main(void)
{
    int entered;
    
    printf("Please enter:\n1 for an unsigned short int\n2 for a short int\n3 for an unsigned long int\n4 for a long int\n5 for an int\n6 for an unsigned int\n7 for a char\n8 for a float\n9 for a double\n");    
    scanf("%d", entered);
    printf("You entered %d...\n", entered);
    
       
    
    if (entered <= 0) {
                printf("Your selection is too low.\nExiting...\n");
                system("PAUSE");
                return 0;
                }
    if (entered == 1) {
                printf("The size of an unsinged short int is %d\n", sizeof(unsigned short int));
                system("PAUSE");
                return 0;
                }
    if (entered == 2) {
                printf("The size of a short int is %d\n", sizeof(short int));
                system("PAUSE");
                return 0;
                }
    if (entered == 3) {
                printf("The size of an unsigned long int is %d\n", sizeof(unsigned long int));
                system("PAUSE");
                return 0;
                }
    if (entered == 4) {
                printf("The size of a long int is %d\n", sizeof(long int));
                system("PAUSE");
                return 0;
                }
    if (entered == 5) {
                printf("The size of an int is %d\n", sizeof(int));
                system("PAUSE");
                return 0;
                }
    if (entered == 6) {
                printf("The size of an unsigned int is %d\n", sizeof(unsigned int));
                system("PAUSE");
                return 0;
                }
    if (entered == 7) {
                printf("The size of a char is %d\n", sizeof(char));
                system("PAUSE");
                return 0;
                }
    if (entered == 8) {
                printf("The size of a float is %d\n", sizeof(float));
                system("PAUSE");
                return 0;
                }
    if (entered == 9) {
                printf("The size of a double is %d\n", sizeof(double));
                system("PAUSE");
                return 0;
                }
    if (entered >= 10) {
                printf("Your selection is too high.\nExiting...\n");
                system("PAUSE");
                return 0;
                }                        
    return 0;
}

Ya, I know there's probably a way to be more efficient than all those if statements…but i don't know it. I also don't need to be told my code sucks. Post if you can help me please. Thanks. end3r


ghost's Avatar
0 0

I didn't check properly what you wanted to do because your code was hard to read, but this is C++ and is fairly simple to get an input and check against a value for output, I think this would be more efficient.

#include <iostream>	

using namespace std;
		
int main()                            
{
  int nums;                           
  
  cout<<"Enter number: ";   
  cin>> nums;                        
  cin.ignore();                       
  if ( nums < 0 ) {                 
     cout<<"Your number is too low!\n"; //
  }
  else if ( nums >= 11 ) {           
     cout<<"Your number is too high\n";         
  }
  else {
    cout<<"Didn't enter a number\n";     
  }
  cin.get();
}

Edit: Don't use system pause(); because it is OS specific to Winblowz and it will become a bad habit.


ghost's Avatar
0 0

lol, thanks flash, but i already knew how to do it in c++. the reason i'm trying to learn c is for kg's learnify things because all of his vuln programs are in c not c++. what is there to use in c apart from system("PAUSE") because cin.get(); only works in c++. anyone else know? Thanks. end3r


ghost's Avatar
0 0

end3r wrote: what is there to use in c apart from system("PAUSE") because cin.get(); only works in c++.

add in c++ then

just put at the top

#include <stdio.h>// is that right?
#include <iostream>

using namespace std;

int main()
{
cin.get(); /*wow*/
}

ghost's Avatar
0 0

nattie wrote: [quote]end3r wrote: what is there to use in c apart from system("PAUSE") because cin.get(); only works in c++.

just put at the top

#include <stdio.h>// is that right?
#include <iostream>

using namespace std;

int main()
{
cin.get(); /*wow*/
}
```[/quote]
nattie, what the fuck don't you get cin.get() only works in C***++***
Your post was completely retarded and useless. At the "/*wow*/" bit were you trying to imply that i asked a stupid question and didn't think of cin.get()?
end3r

ghost's Avatar
0 0

scanf("%d", entered);

scanf needs to get adress as the argument not the value. remember entered is the value while &entered is the adress

so the correct line would be: scanf("%d", &entered);


ghost's Avatar
0 0

Hi, end3r.

You said there must be something more efficient than all those if statements and you're right - there is…

switch entered { case 0: printf ("…"); break; case 1: printf("…"); break; /* etc */ }

return 0;

That should generally save a bit of typing and make things more readable.


ghost's Avatar
0 0

I appreciate the help. end3r