Thread: why won't cin recognize return as a character

  1. #1
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    why won't cin recognize return as a character

    In the following program when it asks for the rate of vat I want to press return and get it to default to 17.5 percent but I can't figure out how to trick cin, if anybody can help it would be appreciated, thanks.

    Code:
    /*Program to work out the price of any quantity
    of products at any price, plus any rate of vat*/
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    void main()
    
    {
    
    	//Declare the variables
    	float cost, quant, price, vat, full, temp;
    	int length;
    	char yesno, vats[10];
    
    		  while (yesno != 'n' && yesno != 'N')
    		  {
    				//Get info from user
    				clrscr();
    				cout<<"Please enter the price of the product: ";
    				cin>>cost;
    				cout<<"\n\n";
    				cout<<"Please enter the quantity of products: ";
    				cin>>quant;
    				cout<<"\n\n";
    				cout<<"Please enter your rate of vat: ";
    				cin>>vats;
    
    				length=strlen(vats);
    
    				cout<<length;
    
    				if (length == 0)
    				{
    					 temp = 17.5;
    				}
    
    				else
    				{
    					 temp=atof(vats);
    				}
    
    
    				cout<<"\n\n\n";
    
    				//do the calculation
    				temp= temp / 100;
    				price= cost * quant;
    				vat= cost * quant * temp;
    				full= price + vat;
    
    
    				//give the answer
    				cout<<"The price of "<<quant<<" product(s) at "<<cost<<" each = £"<<price<<"\n\n"
    				"The vat = £"<<vat<<"\n\n"
    				"Price plus vat = £"<<full<<"\n\n";
    
    				cout<<"Would you like to use the program again? ";
    				cin>>yesno;
    				cout<<"\n";
    
    		  }
    		  clrscr();
    		  cout<<"Thankyou Bye!";
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try initializing vats to an empty string intially and then at the end of every loop before starting over.

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks, I already tried that and it doesn't work, any other suggestions would be very much appreciated.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    No, main returns int.

    >cin>>vats;
    Change this to
    Code:
    cin.ignore();
    cin.getline ( vats, 10 );
    And strlen will return 0 if the user hits enter.

    -Prelude
    My best code is written with the delete key.

  5. #5
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks for that, it works and everything, I think I need to find out more about cin.ignore and cin.getline because last time I had a problem it was to do with thise, I thought cin.getline had to have three expressions? variable name, length and delimiter, how come this one works witout the delimiter?

    Thanks for the void tip but for this program main doesn't return int, I am not getting at you but people seem to get upset about this, for now void main is fine for me, some people hate it others don't mind, programmers are weird.

    Thanks for your help once again.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I thought cin.getline had to have three expressions? variable
    >name, length and delimiter, how come this one works witout the delimiter?
    getline is overloaded, if you omit the delimiting character then getline will default to a newline character '\n'.

    >Thanks for the void tip but for this program main doesn't return int
    The calling program will *always* receive an int from your program, if main doesn't return an int then the value returned to the calling program will be garbage and the resulting behavior is undefined.

    >I am not getting at you but people seem to get upset about
    >this, for now void main is fine for me, some people hate it others don't mind, programmers are weird.
    Those who don't mind are the ones who don't know how bad it is. Undefined behavior means anything could happen, including reformatting your hard drive without you being informed of it. If you use void main then you're asking for trouble, so do yourself a favor and always return int. The most common return from main is 0 for success.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Wink I like to use getline()

    [i] from Prelude
    Code:
    cin.ignore();
    cin.getline ( vats, 10 );
    [/B]
    Since the first time that you adviced me to use this function, and I am using it for most of my input, it is such a great function.

    Thanx Prelude
    C++
    The best

  8. #8
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128
    Thanks prelude, I've never really had the void main() thing explained to like that before, as I say am only a beginner but I do realize that doing things in C++ can nacker the operating system and all that like writing variables to strange areas of memory, but I guess now that someone has explained it properly I will start using return 0, I do sometimes anyway just to keep the method fresh, I still might like to see my hard drive formatted for no reason, and I'm glad to see you didn't argue with my statement that programmers are weird, thanks again.

    UnclePunker.

  9. #9
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    ummmmmm...

    so if i use void main my program could randomly reformat my hd?

  10. #10
    Registered User Kupo's Avatar
    Join Date
    Dec 2001
    Posts
    36
    it could do anything.

    perhaps even create a small hole in fabric of time and transport you back to the 1150's.

    which, despite being lots of fun, probably isn't advisable.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so if i use void main my program could randomly reformat my hd?
    Undefined behavior means that anything could happen. The program could work properly, launch nuclear warheads and throw the world into thermonuclear war (if you have the hardware option installed), or even worse, reach out and pull demons out of your nose.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    [sarcasm]
    Wow, what cool side effects. Never used void main before, but gunna use it all the time now [/sarcasm]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM