Thread: plzzzz help in this validations

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Unhappy plzzzz help in this validations

    I have 2 questions ro solve but I donno how to go about them. I would really appreciate if anyone cam help in any of them!!

    First, how can I validate for non numeric value entered by the user using while loop? for example, if the user enter non numeric value, I should stop the while loop?

    Second, how can I count the number of spaces , the number of new lines , and the number of all other characters entered by the user??


    Finnaly, how can I replace each period with an exclamation mark in a string entered by the user and print it?


    plzzzzzzzzzzzz help!!!!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by moza
    I have 2 questions ro solve but I donno how to go about them. I would really appreciate if anyone cam help in any of them!!

    First, how can I validate for non numeric value entered by the user using while loop? for example, if the user enter non numeric value, I should stop the while loop?
    #1: Test the status of the stream after the extraction operation:
    Code:
    int value;
    cout << "Enter in a bunch of integers: ";
    while( cin >> value )
        cout << "You entered: " << value << endl;
    
    cin.clear();  // Clear streams error state
    string data;
    getline(cin,data);
    
    cout << data << " is not an integer." << endl;
    Possible I/O:
    Code:
    Enter in a bunch of integers: 102 35 16
    You entered: 102
    You entered: 35
    You entered: 16
    56 17
    You entered: 56
    You entered: 17
    18
    You entered: 18
    20blah
    You entered: 20
    blah is not an integer.


    Quote Originally Posted by moza
    Second, how can I count the number of spaces , the number of new lines , and the number of all other characters entered by the user??
    Easiest way: Read character-by-character and insert them into a map<char,int> container or update the value stored there.



    Quote Originally Posted by moza
    Finnaly, how can I replace each period with an exclamation mark in a string entered by the user and print it?
    That's 3 questions, not 2...

    There is a handy dandy replace function in the <algorithm> header or the string container itself has a replace member function... learn how to use them.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Code:
    		while(inputIsInvalid)
    			{
    				cout << endl << "Enter Input > " ;
    				cin >> input;
    				length = strlen(input);//sets length to number of charcters in input
    				if(length > 1)
    				{
    					cout << "input is invalid" << endl;
    				}
    				else if(!isdigit(input[0]))//checks to see if input is a digit
    				{
    					cout << "input is invalid" << endl;
    				}
    				else
    				{
    					i = atoi(input);//converts input into int
    					inputIsInvalid = false;
    				}
    			}

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    thank all of you for the useful reply, ok, I've solved the first problem.

    Could you please explain more solution 2 ? I would really appreciate it!!

    I will search for the replace function and learn it! thanks for the hint!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i need help with my c programm...Plzzzz
    By don bossko in forum C Programming
    Replies: 1
    Last Post: 05-16-2002, 06:21 PM