I am taking a summer course in computer science, and I get assigned two projects a week, so bear with me if I'm getting some terminology wrong.
This week, I was able to get the first one done, but not the second one. In the second assignment, we were required to rewrite the ReadDouble function so that it's more user-friendly; user-friendly as in allowing the user to input commas along with numbers. Additionally,
- 
we were required to allow the first character to be a number, plus or minus sign, or a decimal point.
 - 
All other characters can be a number, comma, or decimal point (if there already wasn't one).
 - 
As mentioned, commas must be written in properly (we can't allow the user to input 10,00.244, 343,2.334, or some nonsensical number like that).
 - 
No commas should be allowed after the decimal point.
 - 
Only one decimal point should be allowed in the number.
 
So far, I was able to able to meet 1), 2), and 5). 3) and 4)? Not so much.
The underlying issue is that I don't know what classes, objects, and whatnot I should be using to make the program read the string input and determine if the commas were inserted properly. I have an idea that I would need to use something similar to the "input.length()" code, set it as a variable that can be compared in an if statement to ensure that the amount of digits until the next comma can be used is met.
I also tried writing a for loop that would check after the decimal place for commas or any other invalid character, but I didn't know what to write down as its initialization. How would I get the for loop to start looking from the decimal after it's aware that one decimal exists?
Another major issue I am encountering is that when I input something like 1.2, it is displayed as 12, meaning that "atof(convert.cstr())" has stripped the decimal from the return value. However, when I enter it as just .2, it comes out as 0.2.
I will provide the code of what I have written so far along with the code of what a friend has suggested to me.
My code:
#include <iostream> 
#include <cstdlib>
#include <string>
#include <climits>
using namespace std;
// Main Menu Error Prototype
double ReadDouble(string prompt);
double ReadDouble(string prompt)
{
    string input;
    string convert;
    bool isValid = true;
    do {
        // Reset the flag to valid input
        isValid = true;
        // Read input from user
        cout << prompt;
        cin >> input;
        // Validate user input
        // Check the first character is a number, + or -
        int decimal = 0;
        if (input[0] != '+' && input[0] != '-' && input[0] != '.' && isdigit(input[0]) == 0) {
            cout << "Error! Input was not an integer.\n";
            isValid = false;
        }
        else {
            if (input[0] == '.') {
                decimal++;
                //cout << "." << endl;
            }
            convert = input.substr(0, 1);
        }
        // check that the remaining characters are numeric
        long len = input.length();
        for (long index = 1; index < len && isValid == true && decimal <= 1; index++) {
            if (input[index] == ',') {
                ;  // do nothing if character is a ','
            }
            else if (input[index] == '.') {
                decimal++; // do nothing if character is a '.'
                if (decimal > 1) {
                    cout << "Error! You can have only one decimal point.\n";
                    isValid = false;
                }
            }
            else if (isdigit(input[index]) == 0) {
                cout << "Error! Input was not an integer.\n";
                isValid = false;
            }
            
            else {
                convert += input.substr(index, 1);
            }
            
        }
        // Start looking where the decimal starts 
        /*
        long decimal=input.find('.');
        for (decimal; decimal < len && isValid==true; decimal++) {
            if (input[decimal] =='.') {
                ; // do nothing if character is a '.'
            }
        }
        */
        //cout << "\nDecimal value is " << decimal << endl; -- Test Code
    } while (isValid == false);
    
    double returnvalue = atof(convert.c_str());
    
    return returnvalue;
}
int main()
{
    double x = ReadDouble("Enter a value: ");
    cout << "Value entered was " << x << endl;
    return 0;
}
My friend's incomplete code:
ReadDouble(){
     isValid = true
     do{
            get user input and set it to a variable called input
            set output variable to a variable called output
            bool hasDecimal = false;
            int digitsUntilNextComma = 3
            for(i = 0; i < input length; i++){
                    if(input[i] == ','){
                             if((i < 3 && i > 0) || digitsUntilNextComma == 0){
                                     digitsUntilNextComma = 3;
                                     output += input[i];
                             }else{ //if it gets to here the comma was in a bad place like ,123 or 12,12,123
                                        isValid = false; 
                                        i = input length  //breaks out of for loop
                              }
                    } else if(input[i] == '.'){
                             if(i < 3 || digitsUntilNextComma == 0){
                                     if(hasDecimal){ //if this is true then the input had 2 decimals 
                                               isValid = false; 
                                               i = input length  //breaks out of for loop
                                      }else{
                                              hasDecimal = true;
                                              digitsUntilNextComma = 3;
                                              output += input[i];
                                       }
                             }else{ //if it gets to here, a previous comma was in a bad place like 12,34.56
                                      isValid = false; 
                                      i = input length  //breaks out of for loop
                              }
                    }else{
                            output += input[i];
                            digitsUntilNextComma--;
                    }
            }
          
     }while(isValid == false)
}
I hope what I provided wasn't too vague or messy. Again, I had little exposure to programming in the past, so forgive me if I mess some terminology up.
Aucun commentaire:
Enregistrer un commentaire