Announcement

Collapse
No announcement yet.

C++ problem, leading zero

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ problem, leading zero

    In my C++ program i am writing the time is taken in.

    The integers hrs and mins are reading the values and the whole program works except for this little problem

    When entering a time such as 9 (for hrs) i cant use a leading zero 0 9 as an error occurs - what causes it?

    Code:
    void getParkTime(int& hrs, int& mins)
    {
    	int h,m;
    	h = 0;
    	m = -1;
    	gotoxy(2,4);
    	clreol();
    	cout<<"Arrival time ";
    	while ((h< 7) || (h > 20))
      	{
          gotoxy(2,5);
          clreol(); // inserted as noted above 3.12.04
          cout<<"hrs:";
          gotoxy(7,5);
          clreol(); // inserted as noted above 3.12.04
          cin>>h;
       }
    	while ((m < 0) || (m> 59))
    	{
       	gotoxy(2,6);
          clreol(); // inserted as noted above 3.12.04
          cout<<"mins:";
          gotoxy(8,6);
          clreol(); // inserted as noted above 3.12.04
          cin>>m;
       }
          hrs = h;
          mins = m;
    }
    cheers for any help i recieve guys

  • #2
    A leading zero usually indicates an octal number, but 9 is not a valid octal digit. Perhaps you could could do something along the lines of:

    Code:
    cin >> setiosflags(ios::dec) >> h;
    Note that I have not tested or verified any of the above.

    Comment


    • #3
      Originally posted by FunkyChicken
      When entering a time such as 9 (for hrs) i cant use a leading zero 0 9 as an error occurs - what causes it?
      In some MS Windows Env, leading zeros for input can be interprited as octal values. What kind of error do you find?

      Doh!
      This time Voltage Spike beat me to it. :-)
      Last edited by TheCotMan; January 13, 2005, 13:30.

      Comment


      • #4
        my code is really crap, so im a liitle imbarrassed to post it.

        Im new to programming so we have all got to start some where.

        Here is the full prog

        it is basically a carpark assignment that im doing for my tutorer. It only make believe but i want it to be as good as i can get it. if you run it, try and enter 09 for the hours

        cheers for your time once again


        Code:
        #include <stdio.h>
        #include <stdlib.h>
        #include <iostream.h>
        #include <conio.h>
        #include <ctype.h>
        #include <time.h>
        #include <math.h>
        #include <dos.h>
        void getParkTime(int& hrs, int& mins)
        {
        	int h,m;
        	h = 0;
        	m = -1;
        	gotoxy(2,4);
        	clreol();
        	cout<<"Arrival time ";
        	while ((h< 7) || (h > 20))
          	{
              gotoxy(2,5);
              clreol(); // inserted as noted above 3.12.04
              cout<<"hrs:";
              gotoxy(7,5);
              clreol(); // inserted as noted above 3.12.04
              cin>>h;
           }
        	while ((m < 0) || (m> 59))
        	{
           	gotoxy(2,6);
              clreol(); // inserted as noted above 3.12.04
              cout<<"mins:";
              gotoxy(8,6);
              clreol(); // inserted as noted above 3.12.04
              cin>>m;
           }
              hrs = h;
              mins = m;
        }
        void showDate()
        {
        	struct date d;
           getdate(&d);
           gotoxy(30,1);
           clreol();
           cout<<"Today's date: "<<(int)d.da_day<< "/"<<(int)d.da_mon<<"/"<<d.da_year;
        }
        void getClock (int& hr, int& min)
        {
        	struct tm *t;
        	time_t lt;
        	lt= time (NULL);
        	t= localtime (&lt);
        	hr= t->tm_hour;
        	min= t->tm_min;
        }
        void ValidateCoin (int coin, bool& pass)
        {
        	if (coin==5||coin==10||coin==20||coin==50||coin==100||coin==200)
        		pass = true;
        	else
        		pass = false;
        }
        void getCoin(int& coin)
        {
        	int entry;
        	gotoxy(2,10);
        	clreol();
        	cout<<"Enter coin:                   ";
        	gotoxy(14,10);
        	clreol();
        	cin>>entry;
        	coin = entry;
        }
        void getValidcoin (int& coin)
        {
        	bool pass = false;
        	while (pass!=true)
           {
           	getCoin(coin);
              ValidateCoin(coin,pass);
           }
        }
        void calcTimeParked (int inhrs, int inmins, int outhrs, int outmins, int& timeparked)
        {
        	timeparked = ((60*outhrs) + outmins)-((60*inhrs) + inmins);
           gotoxy(2,8);
           cout<<"Total time (minutes)";
        }
        void calcInitialFee (int timeparked, int& fee)
        {
        	if (timeparked <= 0) fee = 0;
           else if (timeparked <= 60) fee = 90;
        	else if (timeparked <= 120) fee = 170;
        	else if (timeparked <= 240) fee = 450;
        	else fee = 1000;
        }
        //Procedure updateFee
        void updateFee(int coin, int& fee)
        {
        	fee=fee-coin;
        }
        void putTicket()
        {
        	gotoxy(10,20);
        	clreol();
        	cout<<"Fee paid - Ticket returned ";
        	sleep(4);
        }
        void putFee(int fee)
        {
        	float temp,pounds;
        	int pence;
        	temp = fee;
        	pounds = floor(temp/100);
        	pence = fee%100;
        	gotoxy(25,8);
        	clreol();
        	cout<<"Fee Due  "<<char(156);
          	if (pounds==0)
           	cout<<"0.";
          	else
           	cout<<pounds<<".";
        	if (pence==0)
           	cout<<"00";
        	else if (pence<10)
           	cout<<"0"<<pence;
        	else
           	cout<<pence;
        }
        void deliverChange(int change)
        {
        	int twohundred = 0;
           int hundred = 0;
           int fifty = 0;
           int twenty = 0;
           int ten = 0;
           int five = 0;
           float changepounds = 0;
         	clreol();
           change = abs(change);
           changepounds = change;
           changepounds = changepounds/100;
           gotoxy(2,12);
           cout<<"Your change is: "<<changepounds<<"0";
        	gotoxy(22,12);
        	cout<<"pounds";
           gotoxy(38,12);
           cout<<"-Coin-";
           gotoxy(50,12);
           cout<<"-Quantity-";
         	while (change >= 200)
           {
         		twohundred = twohundred + 1;
         		change = change - 200;
        	}
         	while (change >= 100)
           {
         		hundred = hundred + 1;
         		change = change - 100;
           }
         	while (change >= 50)
           {
         		fifty = fifty + 1;
         		change = change - 50;
         	}
         	while (change >= 20)
           {
         		twenty = twenty + 1;
         		change = change - 20;
         	}
         	while (change >= 10)
           {
         		ten = ten + 1;
         		change = change - 10;
         	}
         	while (change >= 5)
           {
         		five = five + 1;
         		change = change - 5;
         	}
           gotoxy(40,13);
           cout<<char(156)<<"2";
           gotoxy(55,13);
           cout<<twohundred;
        
           gotoxy(40,14);
           cout<<char(156)<<"1";
           gotoxy(55,14);
           cout<<hundred;
        
           gotoxy(40,15);
           cout<<"50p";
           gotoxy(55,15);
           cout<<fifty;
        
           gotoxy(40,16);
           cout<<"20p";
           gotoxy(55,16);
           cout<<twenty;
        
           gotoxy(40,17);
           cout<<"10p";
           gotoxy(55,17);
           cout<<ten;
        
           gotoxy(40,18);
           cout<<"5p";
           gotoxy(55,18);
           cout<<five;
        
        }
        // ------------------------------------------------------
        // 'Executive' module is main( )-- code below -----------
        void main()
        {
        	int inhrs, inmins, outhrs, outmins, timeparked, fee, coin;// declare local variables
           char temp;   // keep this additional one for use with the simulation though not in design
           //textbackground(WHITE);
           clrscr();
           gotoxy(2,2);
           textcolor(BLUE);
           clreol();
           cout<<"Enter ticket (q to quit) ";
           showDate();
           _setcursortype(_NOCURSOR);
           temp=tolower(getch());
           while (temp !='q')
           {
           	getParkTime (inhrs,inmins);
           	getClock (outhrs, outmins);
           	calcTimeParked (inhrs, inmins, outhrs, outmins, timeparked);
            	calcInitialFee (timeparked, fee);
            	while (fee>0)
              {
            		putFee(fee);
            		getValidcoin(coin);
           	  	updateFee(coin,fee);
            	}
            	putFee(0);
            	if (fee!=0)
            		deliverChange(fee);
            		putTicket();
            		clrscr();
            		gotoxy(2,2);
              	cout<<"Enter ticket (q to quit) ";
            		showDate();
            		temp=tolower(getch());
           }
        }

        Comment

        Working...
        X