Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,104 people online right now. Registration is fast and FREE... Join Now!




Strings

 
Reply to this topicStart new topic

Strings

csstarter9
20 Oct, 2006 - 05:07 AM
Post #1

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
This is my program and what it is to do:

QUOTE

Write a program that asks the user to enter a single real (float) number (positive or negative) and outputs twice the number the user entered.
The program must validate the input of the number by first storing it into a character string and then verifying that it only contains digits, decimal point, and negative sign.
Limit the size of the array to 30 characters.
Verify that the maximum number of decimal points in the string is one (1).
Verify that the negative sign (if any) can only be at the first position of the string.
If any of the validation conditions fail, notify the user and request the number again until it is correct.
Once the string is validated, convert the string to float and output the number multiplied by two.
There is no requirement for this program to contain additional functions (other than main) or to request the user if he/she would want to enter a number again after validated.


This is what i have so far, am i off to a good start and what advise or help could you give me.

CODE

#include <iostream>


using namespace std;

int main()
{
    char s0;
    char s1[30];
    cout << "Enter a number: ";
    s0 = cin.get();
    cin.get(s1,30);
    cout << s0 << "\n";
    cout << s1 << "\n\n";

    char s2[30];
    char s3[30];
    char s4[30];
    cout << "Enter a number: ";
    cin.getline(s2,30,' ');
    cin.getline(s3,30,' ');
    cin.getline(s4,30,' ');
    cout << s2 << s3 << s4 << "\n\n";
    
    char s5[30];
    char s6[30];
    char s7[30];
    cout << "Enter a number: ";
    cin.get(s5,30,' ');
    cin.get(s6,30,' ');
    cin.get(s7,30,' ');
    cout << s5 << s6 << s7 << "\n\n";

    return 0;
}

User is offlineProfile CardPM
+Quote Post

rockstar_
RE: Strings
20 Oct, 2006 - 01:54 PM
Post #2

D.I.C Head
Group Icon

Joined: 16 Oct, 2006
Posts: 187


Dream Kudos: 275
My Contributions
CODE

    char s2[30];
    char s3[30];
    char s4[30];
    cout << "Enter a number: ";
    cin.getline(s2,30,' ');
    cin.getline(s3,30,' ');
    cin.getline(s4,30,' ');
    cout << s2 << s3 << s4 << "\n\n";
    
    char s5[30];
    char s6[30];
    char s7[30];
    cout << "Enter a number: ";
    cin.get(s5,30,' ');
    cin.get(s6,30,' ');
    cin.get(s7,30,' ');
    cout << s5 << s6 << s7 << "\n\n";

Well, yes, you're on the right track. What is the above code for? You only need to grab one number as a string from the user. Then you're supposed to validate that string against the requirements, reporting errors if they occur and then printing out 2x the number put in. That's it. So you don't need that code. Validate only that one variable, and then multiply it by two and print out the result.
User is offlineProfile CardPM
+Quote Post

csstarter9
RE: Strings
23 Oct, 2006 - 05:17 AM
Post #3

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
QUOTE(rockstar_ @ 20 Oct, 2006 - 02:54 PM) *

CODE

    char s2[30];
    char s3[30];
    char s4[30];
    cout << "Enter a number: ";
    cin.getline(s2,30,' ');
    cin.getline(s3,30,' ');
    cin.getline(s4,30,' ');
    cout << s2 << s3 << s4 << "\n\n";
    
    char s5[30];
    char s6[30];
    char s7[30];
    cout << "Enter a number: ";
    cin.get(s5,30,' ');
    cin.get(s6,30,' ');
    cin.get(s7,30,' ');
    cout << s5 << s6 << s7 << "\n\n";

Well, yes, you're on the right track. What is the above code for? You only need to grab one number as a string from the user. Then you're supposed to validate that string against the requirements, reporting errors if they occur and then printing out 2x the number put in. That's it. So you don't need that code. Validate only that one variable, and then multiply it by two and print out the result.



so it would look like this?
CODE

#include <iostream>


using namespace std;

int main()
{
    char s0;
    char s1[30];
    cout << "Enter a number: ";
    s0 = cin.get();
    cin.get(s1,30);
    cout << s0 << "\n";
    cout << s1 << "\n\n";


and then what?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings
23 Oct, 2006 - 08:26 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Now that you've taken the input, you need to check what was provided. You need to traverse the character array and ensure tha parameters have been met. Check and see if the first element is either a negative sign or a digit...if not, generate an error and reprompt. While traversing the array, keep a count of how many decimal places you encounter...if more than one, generate an error and reprompt. If it passes, convert and multiply by two. you may widh to chec out the following functions:

http://www.cprogramming.com/fod/isdigit.html

http://www.cplusplus.com/ref/cstdlib/strtod.html

http://www.cplusplus.com/ref/cstdlib/atof.html

It should be noted that the use of the atof() function can be problematic, however, since it would return 0.0 for both a coversion of '0.0' and an error. since you're validating upfront, however, you'l; be fine.
User is offlineProfile CardPM
+Quote Post

csstarter9
RE: Strings
25 Oct, 2006 - 07:34 AM
Post #5

New D.I.C Head
*

Joined: 18 Sep, 2006
Posts: 21


My Contributions
Ok i've worked on it some more and this is what i have but i keep getting an error message:
CODE

#include <iostream>

using namespace std;


bool dot(char *sNumb)
{
    static dot_count=0;

    if(*sNumb=='.')
        dot_count+=1;
    else
        return false;
    return dot_count<=1;
}

bool checkvalid(char *sNumb)
{

    int n=strlen(sNumb);
    int i=0;
    if(*sNumb=='+' || *sNumb=='-')
    {
        if(n==1)
            return false;
        i=1;
        sNumb+=1;
    }
    else
        i=0;
    
    for(i;i<n;i++,sNumb++)
        if (!(*sNumb>='0' && *sNumb<='9'))
        {
            if(n==1)
                return false;
            
            if(!dot(sNumb))
                return false;
        }
return true;
}

bool readnumer(float &fNumber)
{
    char *sNumb=new char[30];
    cout<<"Enter a number:";
    cin>>sNumb;
    if(checkvalid(sNumb)==true)  
    {
        fNumber=atof(sNumb);
        delete sNumb;
    }
    else
    {                
        delete sNumb;
        return false;
    }



    return true;
}
int main()
{
    float fNumber;

    while(!readnumer(fNumber))  
        cout<<"Invalid float number. Please try again!\n";
    cout<<"Twice your number is:"<<fNumber*2;
    return 0;
}


any help would be nice thank you
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Strings
26 Oct, 2006 - 04:19 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
What is the error message you are receiving?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month