char f;
cin >> f;
Oh Wait You Want Integer so It Would Be int f;
cin >> f;
I'm pretty sure there is a very simple way to do this but I haven't found a good explanation for how to do it. How would I go about getting an input from a user that I can verify is an integer?
One other question I have is related to code similar to this:
Now thats the code I have for finding factors of a number. I would want to add a percent type thing to it but I'm not sure how I would do that. I could divide 'loops' by 'innum' but how could I control how many decimal places are kept if i wanted to display something like xx.xx%Code:int factor(long innum) { int prev = 0; ofstream factors; factors.open("factors.txt"); if (factors.is_open()) { factors << "Factors of " << innum << ":" << endl << endl; for (long loops = 1; loops <= innum; loops++) { if (innum % loops == 0) { factors << loops << " "; prev++; } if (prev == 7) { factors << endl; prev = 0; } } } else { clearscreen wait cout << "Error opening file!!!" << endl << "Exiting..."; return(1); } factors.close(); return(0); }
Thanks for any help in advance
char f;
cin >> f;
Oh Wait You Want Integer so It Would Be int f;
cin >> f;
Last edited by whit; 09-25-2010 at 12:37 PM.
hobosrock696 (09-25-2010)
Yea but now how do I now take it so it can be manipulated as a number and how do I verify there are no letters in it?
Edit: I wrote that code myself I do know what I am doing to this level I am asking how do I get input from the user and extract a number from it but also making sure that nothing but numbers were typed,
I think he want to make sure that the user don't type in a letter or smth.
One Solution:
i think this is ONE solution but there should be millions of ways you could do itCode:char input; cin>>input; testinputaboutascii(); Convert char to int (sprintf should work)
ASCII codes allows would be 0x41 - 0x5A (A-Z) and 0x61 - 0x7A (a-z)
check the char input with a pointer the the char and then go one step forward
if(*char==......)
char++;
Last edited by doofbla; 09-25-2010 at 01:09 PM.
_____________________________________________
READING TUTORIAL:
1. READ MY POST
2. THINK ABOUT MY POST
3. PRESS THANKS
4. MAYBE CORRECT MY POSTS :P
Dijkstra:
"Computer Science is no more about computers than astronomy is about
telescopes."
THANKS BUTTON RIGHT DOWN --->
hobosrock696 (09-25-2010)
doofbla (09-25-2010)
Last edited by doofbla; 09-25-2010 at 01:48 PM.
_____________________________________________
READING TUTORIAL:
1. READ MY POST
2. THINK ABOUT MY POST
3. PRESS THANKS
4. MAYBE CORRECT MY POSTS :P
Dijkstra:
"Computer Science is no more about computers than astronomy is about
telescopes."
THANKS BUTTON RIGHT DOWN --->
I have done pointers and memory all that jazz im not great with it but this I can handle. Yes I consider myself a noob and will continue to until I know c++ very well and at least a bit of the windows api + direct x
Question:
Would I check for ascii? Or is it unicode? Is it different for linux or for macs?
Last edited by hobosrock696; 09-25-2010 at 02:24 PM.
Ok so I tried writing some code but obviosly im doing something wrong since it doesnt work. Here it is....
Oh my noob self forgot to add the * before the pointer. Problem is It still wont compile... Anyone willing to write this into a WORKING example XD and thanks for any help once again!Code:// testingchar.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; int main() { char * input = "a"; int counter = 0; cin.getline(input, 20); while (input != "\0") { cout << input[counter] << endl; counter++; } return 0; }
Last edited by hobosrock696; 09-25-2010 at 03:24 PM.
hobosrock696 (09-26-2010)
Ok i made it for you -.- but i think next time you should show more self engagement:
with this you can proof if the user entered only numbers -> in ASCII it worksCode:#include "windows.h" #include "stdio.h" #include "iostream" using namespace std; char input[20]; bool checkchar(char * in) { while(*in != 0) { if(*in < 0x30 || *in > 0x39) { return false; } in++; } return true; } int main() { cin.getline(input, 20); if(checkchar(input)) { cout<<"You entered only numbers - OK"<<endl; } else { cout<<"You entered NOT only numbers - FAIL"<<endl; } system("PAUSE"); return 0; }
_____________________________________________
READING TUTORIAL:
1. READ MY POST
2. THINK ABOUT MY POST
3. PRESS THANKS
4. MAYBE CORRECT MY POSTS :P
Dijkstra:
"Computer Science is no more about computers than astronomy is about
telescopes."
THANKS BUTTON RIGHT DOWN --->
hobosrock696 (09-26-2010)
Thanks and well this is why I am a noob. Also yes thats true I didnt think about that much I apologize for being.... a sort of leecher in this case
Edit: Shouldnt you also not be checking for 0 but "\0" which signifies the end of a string of chars? Or is that just me being wrong?
Last edited by hobosrock696; 09-26-2010 at 10:15 AM.
just make a for cicle and check if the variable of the loop is equal to your imput, like:
int f;
cin>>f;
for(int j=0; j<10; j++)
if(f==j)
//it's a number
List Of My Public Releases
Wallhack and NoFog - Detected (https://www.mpgh.net/forum/164-combat...ml#post1391299)
BOSCHACK CA EU v1.0 - Detected (https://www.mpgh.net/forum/172-combat...eu-v1-0-a.html)
BOSCHACK CA EU v1.1 - Detected (https://www.mpgh.net/forum/172-combat...ml#post2371767)
BOSCHACK Returned - Detected (https://www.mpgh.net/forum/172-combat...eturn-pub.html)
Make Hacks In 5min in 3 Steps
Yea I was looking at the code. I'm assuming the compiler would optimize that into the same code in the end but I still prefer a for over a while loop in which a variable is incremented.
_____________________________________________
READING TUTORIAL:
1. READ MY POST
2. THINK ABOUT MY POST
3. PRESS THANKS
4. MAYBE CORRECT MY POSTS :P
Dijkstra:
"Computer Science is no more about computers than astronomy is about
telescopes."
THANKS BUTTON RIGHT DOWN --->