Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow

    Simple c++ help needed

    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:
    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);
    }
    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%

    Thanks for any help in advance

  2. #2
    whit's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    7,159
    Reputation
    490
    Thanks
    2,253
    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.

  3. The Following User Says Thank You to whit For This Useful Post:

    hobosrock696 (09-25-2010)

  4. #3
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    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,

  5. #4
    whit's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    7,159
    Reputation
    490
    Thanks
    2,253
    Quote Originally Posted by hobosrock696 View Post
    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,
    Soon As They Type In The Integer For f Its Goin to Be Saved In So Yea

  6. #5
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    I think he want to make sure that the user don't type in a letter or smth.

    One Solution:
    Code:
     char input;
    cin>>input;
    testinputaboutascii();
    Convert char to int (sprintf should work)
    i think this is ONE solution but there should be millions of ways you could do it

    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 --->

  7. The Following User Says Thank You to doofbla For This Useful Post:

    hobosrock696 (09-25-2010)

  8. #6
    whit's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    7,159
    Reputation
    490
    Thanks
    2,253
    Quote Originally Posted by doofbla View Post
    I think he want to make sure that the user don't type in a letter or smth.

    One Solution:
    Code:
     char input;
    cin>>input;
    testinputaboutascii();
    Convert char to int (sprintf should work)
    i think this is ONE solution but there should be millions of ways you could do it

    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++;
    He Said He Was No0b He Isnt Goin To Know How Too Do All That...Atleast I Think

  9. The Following User Says Thank You to whit For This Useful Post:

    doofbla (09-25-2010)

  10. #7
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    Quote Originally Posted by whit View Post


    He Said He Was No0b He Isnt Goin To Know How Too Do All That...Atleast I Think
    Ah ok I didn't think about that so.. ty.

    Anyhow I've a new idea how to do:

    Maybe you can just disable all keys instead of 0-9 for the time of input

    There are tons of ways
    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 --->

  11. #8
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    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.

  12. #9
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    Ok so I tried writing some code but obviosly im doing something wrong since it doesnt work. Here it is....
    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;
    }
    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!
    Last edited by hobosrock696; 09-25-2010 at 03:24 PM.

  13. #10
    Toxic Waltz's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    114
    Reputation
    14
    Thanks
    18
    Quote Originally Posted by hobosrock696 View Post
    Ok so I tried writing some code but obviosly im doing something wrong since it doesnt work. Here it is....
    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;
    }
    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!
    define input as an array.
    char only holds 1 character

  14. The Following User Says Thank You to Toxic Waltz For This Useful Post:

    hobosrock696 (09-26-2010)

  15. #11
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    Ok i made it for you -.- but i think next time you should show more self engagement:
    Code:
    #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;
    }
    with this you can proof if the user entered only numbers -> in ASCII it works
    _____________________________________________

    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 --->

  16. The Following User Says Thank You to doofbla For This Useful Post:

    hobosrock696 (09-26-2010)

  17. #12
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    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.

  18. #13
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    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

  19. #14
    hobosrock696's Avatar
    Join Date
    Aug 2010
    Gender
    male
    Posts
    45
    Reputation
    9
    Thanks
    1
    My Mood
    Mellow
    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.

  20. #15
    doofbla's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    Biel*****/Germany
    Posts
    369
    Reputation
    10
    Thanks
    179
    My Mood
    Psychedelic
    Quote Originally Posted by hobosrock696 View Post
    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?
    0x48 = 0
    0x49 = 1
    .
    .
    .
    0x57 = 9

    0x0 is a what you get whan you press SPACE
    _____________________________________________

    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 --->

Page 1 of 2 12 LastLast

Similar Threads

  1. [Help Request] Help Needed with injecting hacks?
    By ShoeStrinqs in forum CrossFire Help
    Replies: 3
    Last Post: 09-01-2011, 07:50 PM
  2. [Help Request] Texture won't work, help needed :(
    By Kikimo900 in forum Combat Arms Mod Help
    Replies: 1
    Last Post: 08-29-2011, 01:28 AM
  3. [Help Request] Help needed!
    By FailBladez in forum Combat Arms Help
    Replies: 8
    Last Post: 08-06-2011, 04:37 AM
  4. [Help Request] Some help needed
    By Julma Henri in forum Combat Arms Mod Help
    Replies: 0
    Last Post: 06-28-2011, 01:25 AM
  5. Simple Chams Help Needed Plz.
    By donbiggy in forum Combat Arms Hacks & Cheats
    Replies: 1
    Last Post: 02-27-2009, 02:04 PM