Hello,
I need to a make a function in C which will keep looping onlt if the input is numbers or + I want the input to be numbers cause I want to do something with them. Please help
Regards,
This is IN C
16 Replies - 509 Views - Last Post: 23 September 2011 - 07:28 AM
#1
loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 03:03 AM
Replies To: loop that will keep looping only if numbers or + is entered
#2
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 03:27 AM
Well, there is a isdigit() function but it will only return true if input is 0, 1, 2,3,4,5,6,7,8 or 9. Otherwise false. But you can play with it and find out more.
#3
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 03:44 AM
Please Help Not sure what to do I have tried manythings but I cant seem to work it out
#4
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:02 AM
I have this example in C++. However i am not testing whether its a number cuz im assuming the user will input the number.
Spoiler
This post has been edited by darek9576: 23 September 2011 - 04:09 AM
#5
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:10 AM
but I want to still loop even if + is entered so it should loop still.
#6
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:12 AM
So basically the programs has to DO the input WHILE the input EQUALS a digit OR a plus sign?
#7
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:14 AM
Yes Yes Yes Yes Yes but how do it I have tried many things HELP HELP HELP
BUT YES THATS WHAT IT HAS TO DO
BUT YES THATS WHAT IT HAS TO DO
#8
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:31 AM
Please help
#9
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:34 AM
You could post all the methods you tried so far, could save some time.
#10
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:44 AM
This does not store anywhere your entries but you could use a vector for it however, i dont know vectors so you need to look it up and it stops also when user presses +. Add an OR clause at the end of the loop.
Good luck.
Good luck.
Spoiler
This post has been edited by darek9576: 23 September 2011 - 04:47 AM
#11
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:49 AM
NO NO it should continue to loop even when a plus sign is added or digit anything else it should stop. What I did was I talk the input as string and decalred flag then if strcmp(input,"+") is true flag is ture and if number then true but does not work. I have tried many ways. I just want to basically loop if a number or plus sign is entered.
P.S. EVERYONE THANKS ALOT FOR YOUR HELP I THINK I AM NEARLY THERE
P.S. EVERYONE THANKS ALOT FOR YOUR HELP I THINK I AM NEARLY THERE
#12
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:49 AM
The answer I gave in your other thread is pretty much all you need. Just put the code in a while loop and add some sort of boolean flag which can be used as the condition for your loop. It's very trivial to expand the code to do that.
#13
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 04:55 AM
But man I think aphex19 correct me if I am wrong you are taking one signle input from user like for example 12+1212121231 which correct but I want to scanf values in a loop and then check them. If they contain a number or plus sign if they do the loop continues else loop is terminated.
#14
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 05:14 AM
assert©, on 23 September 2011 - 04:49 AM, said:
NO NO it should continue to loop even when a plus sign is added or digit anything else it should stop. What I did was I talk the input as string and decalred flag then if strcmp(input,"+") is true flag is ture and if number then true but does not work. I have tried many ways. I just want to basically loop if a number or plus sign is entered.
P.S. EVERYONE THANKS ALOT FOR YOUR HELP I THINK I AM NEARLY THERE
P.S. EVERYONE THANKS ALOT FOR YOUR HELP I THINK I AM NEARLY THERE
I know it should loop when + is entered and thats what i said in my post. Add one condition in my code and you are done. THen show us what you have produced.
#15
Re: loop that will keep looping only if numbers or + is entered
Posted 23 September 2011 - 05:27 AM
I had to learnt C++ to try your code okay got it working for c++ but I need the same concept in C here is the code in C++ thanks man
[code]
int main()
{
char input;
do{
cout << "Type in something" << endl;
cin >> input;
cout << input << endl;
}while( isdigit(input) !! input=='+');
cout << "Finishing the program" << endl;
cin.ignore();
cin.get();
return 0;
}
Thanks works the only thing left is translating in C and also converting the character if number into a number
[code]
int main()
{
char input;
do{
cout << "Type in something" << endl;
cin >> input;
cout << input << endl;
}while( isdigit(input) !! input=='+');
cout << "Finishing the program" << endl;
cin.ignore();
cin.get();
return 0;
}
Thanks works the only thing left is translating in C and also converting the character if number into a number
|
|

New Topic/Question
Reply




MultiQuote




|