Just curious.

Ok so I was wondering if it was possible to make it so if my while looop variable isn't a number any number in general it would looop back around and do it again until someone enteres a number instead of entering say the letter 'q' and closing down my program. Any information will be greatly appreciated.

Thank you in advanced.

example;
1
2
3
4
5
6
7
8
printf("\nPlease enter the hours you worked on Tuesday: ");//Hours Worked.
scanf("%f%*c",&Tuesday);

	while(Tuesday > 12)//THIS IS WHERE I NEED TO KNOW WEATHER OR NOT I CAN CHANGE IT SO IT HAS TO BE A NUMBER OTHERWISE LOOP.
		{
			printf("\nPlease enter the hours you worked on Tuesday: ");
			scanf("%f%*c",&Tuesday);
		}
You could use the header <ctype.h> in which case you would write your statement as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include <ctype.h>
using namespace std;

int main()
{

    char num;

    cin >> num;
    while(isdigit(num))
    {
        cin >> num;
    }
 
    return 0;
}
Sorry i'm still new to programming :(,

I have to use '#include<stdio.h>' and only that along with 'void main()' and only that, not sure if that makes a difference.
Last edited on
It looks like your trying to have the user re-enter the "hours worked" if they enter anything over 12. You wouldn't need to use a letter to escape this loop. I'd suggest using a do while loop instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

int main( void ) {

   float tuesday;

   do {  
      printf( "\nPlease enter the hours you worked on Tuesday : " );
      scanf( "%f", &tuesday );

   } while ( tuesday > 12 );  /* read as "do this block of code while tuesday is greater than 12 */


   return 0;
}


A do while loop is great for this kind of thing because it will always DO the do { } code before it checks the while condition. This gives your user the opportunity to enter the time and THEN check it.
Yeah I want it to loop if they enter anything over 12, but I also want it to loop if they enter in anything that isn't a number, for example ; 'a' instead of 12 or 'hello' instead of a number. Is it possible to do that? Iv'e looked in many places but cannot seem to find it.
Indeed it's possible.

1, tuesday is char[], not float. *hint hint*
2, Get yourself a float, named hours (for instance).
3, Allocate a bool named doit, and set it to true.
4, Fill tuesday with a string from scanf.
5, Check every character if it is a digit or punctuation mark.
6, If so, create a for loop to translate characters to digits and a floating point. Then define doit = (hours > 12)
8, THERE CAN ONLY BE ONE HIGHLANDER!
9, If not, do nothing.
10, Use while(doit)
11, Did you notice that I skipped 7?

-Albatross

Last edited on
I'm sorry iv'e never used bool, is there any chance you can show me an example using

#include<stdio.h>

void main()

{

}

Thank you in advanced.
But... I like forcing people to think! All well, I'll just insert a few comments...

(This is very roundabout. I suspect there are simpler ways to solve your problem, but this should get you in the right direction.)

(Gee, if you could use <stdlib.h> and/or <ctype.h>, that would be so much easier...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>

void main()
{
    char[32] tuesday;
    float hours;
    bool doit = true;
    bool skipcheck = false;
    printf("Yo! Slave! How many hours did you work on Tuesday?!?\n");
    scanf("%s", tuesday);
    for(int i = 0; i<32: i++)
    {
       //Check if the tuesday[i] == (a digit character | a character used as a decimal point)
       //In the event that this is not the case, set skipcheck to true.
    }
    //Find where the decimal point is, if there is one, here. Store this value. For loop recommended.
    for(int i = 0; i<32; i++)
    {
       /*
You need a long chain of if statements to check the character... 
...and add the corresponding number times 10^(Distance from Decimal point-1) to hours about here. 
The distance is negative to the right of the decimal point.
      */
    }
    if(!skipcheck)
    {
       doit = (hours > 12);
    }
    while (doit)
    {
      //Your code here.
    }
}


-Albatross
Last edited on
Topic archived. No new replies allowed.