Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 windows says there is an ERROR!?
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
lindsay




PostPosted: Fri Oct 19, 2007 8:49 am   Post subject: windows says there is an ERROR!?

I wrote a program and when a execute it, windows says there is an error and finishs the program then asks if I want to report that error. I cannot figure out what's wrong. I'll appreciate your help. My program is as follows:

code:

#include<stdio.h>
#include<conio.h>
int main()
{
double hour;
char type;
printf("enter the type of your vehicle : for motorcyle m,for outomobile o");
scanf("%c",&type);
  if(type=='m')
  {
               
                 printf("enter the hour:");
                 scanf("%f",&hour);
                 if(hour>0 && hour<2)
                 printf(" cost is 1$");
                 else if(hour>3 && hour<5)
                 printf("cost is 2$");
                 else
                 printf("cost is 3$");
  }

 else
 {
 
            printf("enter the hour:");
                 scanf("%f",&hour);
                 if(hour>0 && hour<2)
                 printf(" cost is 2$");
                 else if(hour>3 && hour<5)
                 printf("cost is 5$");
                 else
                 printf("cost is 6$");
 }
 getch();
 return 0;
}


Mod edit: USE CODE TAGS
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Oct 19, 2007 11:08 am   Post subject: RE:windows says there is an ERROR!?

You are not putting the "conio.h" header to any good use. Get rid of it and the call to "getch" you use to pause your program.
CoryK




PostPosted: Mon Feb 04, 2008 3:27 pm   Post subject: Re: windows says there is an ERROR!?

When you are asked to send the info or not send the info, there should be a way to view what is being sent. This will tell you the issue, it is likely a segmentation fault or the likes (I had the same happen to me once)

code:
#include<stdio.h>
int main()
{
int hour = 0;
char type;
printf("enter the type of your vehicle : for motorcyle m,for outomobile o");
scanf("%c",&type);
  if(type=='m')
  {
               
                 printf("enter the hour:");
                 scanf("%d",&hour);
                 if(hour>0 && hour<2) // Will only work for the value 1.
                 printf(" cost is 1$");
                 else if(hour>3 && hour<5) // Finds the number 4 only.
                 printf("cost is 2$");
                 else
                 printf("cost is 3$"); // Is output for everything but 1 or 4.
  }

 else
 {
 
            printf("enter the hour:");
                 scanf("%f",&hour);
                 if(hour>0 && hour<2) // Same thing for this section.
                 printf(" cost is 2$");
                 else if(hour>3 && hour<5)
                 printf("cost is 5$");
                 else
                 printf("cost is 6$");
 }
 return 0;


Note the comments. I also changed the type from double to int. This will allow you to work with integers instead of floating point numbers. on Linux it works no issue. (That's not to say it will work on Windows)

Hope that helps.

CoryK
btiffin




PostPosted: Mon Feb 04, 2008 4:48 pm   Post subject: Re: windows says there is an ERROR!?

Either change hour to float or use scanf's "%lf" for long float (double) data

Cheers,
Brian
wtd




PostPosted: Mon Feb 04, 2008 4:54 pm   Post subject: RE:windows says there is an ERROR!?

Kinda necro posting here people.
Nick




PostPosted: Mon Feb 04, 2008 4:59 pm   Post subject: RE:windows says there is an ERROR!?

I don't think it's a bad thing wtd... isn't a rule of necroposting saying that if it contains useful information it's allowed, after all this is a site to learn and it should stop a user posting a thread having the same problem... just don't get too off topic
md




PostPosted: Mon Feb 04, 2008 5:24 pm   Post subject: RE:windows says there is an ERROR!?

While I agree that posting useful information is good; posting useful information in responce to a very old question is mostly useless.

Locked.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: