C Program Error
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
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.
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