Question

In: Computer Science

Using c++ Im trying to figure out a good user validation for this loop. So that...

Using c++

Im trying to figure out a good user validation for this loop. So that if a valid number is not input, it gives an error message. I have the error message printing but its creates an infinite loop. Need some help.

double get_fahrenheit(double c){

    double f;

    f = (c * 9.0) / 5.0 + 32;

    return f;  

}

#include "question2.h"

#include<iostream>

#include<string>

using std::cout; using std::cin; using std::string;

int main()

{

    

    double c;

    double f;

    double user_exit = 444;

    do{

        cout<<"Enter Celsius (Enter 444 to exit)\n";

        cin>>c;

        if(cin.good()){

             f = get_fahrenheit(c);

             cout<<c<<" degrees celsius is "<<f<<" degrees fahrenheit. \n\n";

            break;

        }else {

            cout<<"Invalid Input! Please input a numerical value. \n";

            cin.clear();

        }

    }while(c != user_exit);

    cout<<"Goodbye!";

    

    

    

    return 0;

}

Solutions

Expert Solution

i hope this will solve the above problem ... i like would be helpful... comment if any thing goes wrong...

Code:

#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;
double c;

double get_fahrenheit(double c)
{
    double f;
    f = (c * 9.0) / 5.0 + 32;
    return f;
}

int digit(string str)
{
    string dot=".";
    int len = str.size(), j = 0,pos;
    for (int i = 0; i < len; i++)
    {
        if (isdigit(str[i]))
        {
            continue;
        }
        else if(str.find('.')!=pos){
            j++;
            if(j>1)
                return 0;
        }
        else
        {
            return 0;
        }
    }
    c = stod(str);
    return 1;
}

double check(string str)
{
    c = stod(str);
}

int main()

{
    string str;
    int ch;
    double f;
    double user_exit = 444;
    do
    {
        cout << "Enter Celsius (Enter 444 to exit)\n";
        cin >> str;
        ch=digit(str);
        if (c==user_exit)
        {
            break;
        }
        else if (ch == 1)
        {
            f = get_fahrenheit(c);
            cout << c << " degrees celsius is " << f << " degrees fahrenheit. \n\n";
        }
        else
        {
            cout << "Invalid Input! Please input a numerical value. \n";
        }
    }while (c != user_exit);
    cout << "Goodbye!";
    return 0;
}

Output:

Enter Celsius (Enter 444 to exit)
rock
Invalid Input! Please input a numerical value.
Enter Celsius (Enter 444 to exit)
55
55 degrees celsius is 131 degrees fahrenheit.

Enter Celsius (Enter 444 to exit)
55.5
55.5 degrees celsius is 131.9 degrees fahrenheit.

Enter Celsius (Enter 444 to exit)
444
Goodbye!


Related Solutions

Im trying to figure out the errors as to why these test case will not work...
Im trying to figure out the errors as to why these test case will not work for myvector.h. The issue I have with these test cases is the fact that when I implement these test cases into a grader the program declares that it is not working. So I need help in myvector.h to make the test cases work for my function. myvector.h #pragma once #include // print debugging #include // malloc, free using namespace std; template class myvector {...
Im trying to figure out how to draw this Now, in the innermost circle (0.1 m...
Im trying to figure out how to draw this Now, in the innermost circle (0.1 m radius), assume we found 8 species of plant. In the second smallest circle, we found 10 species. In the third smallest circle, we found 13 species. In the largest circle, we found 14 species. Note that when moving from the smallest circle to the next-smallest circle, we did NOT find 10 additional species...The 10 species in the second smallest circle includes the 8 found...
. Use a validation loop to prompt for and get from the user a string that...
. Use a validation loop to prompt for and get from the user a string that is up to ten characters in length. Create value function encodedString that takes the string as input and returns the equivalent encoded string. Start with an empty encoded string. Use a for loop to encode each character of the input string. Use the following algorithm to encode a character and append the encoded character to the encoded string:
I am trying to figure out how to properly execute a while loop and a do/while...
I am trying to figure out how to properly execute a while loop and a do/while loop in the same program using java. I am stuck at just writing the while loop. I made a condition but I can't figure out how to get it to loop if the condition is true?? Also I have to write a do/while loop in the same program. here are the details: This lab demonstrates the use of the While Loop and the Do...
Im asked to edit the C++ code given to me so that the user can enter...
Im asked to edit the C++ code given to me so that the user can enter as many courses as they would like. I've already built the array to house the courses, but Im a tiny bit stuck on writing the user input to the array every time a new line Is made. Code: //CSC 211 Spring 2019 //Program Description: // // Originally From: // CSC 211 Spring 2018 // Dr. Sturm // This program... // #include<iostream> #include<string> #include<fstream> using...
When trying to figure out the capital carrying loss for the next year using an income...
When trying to figure out the capital carrying loss for the next year using an income statement, is it just the negative net income?
[USING R & dataset “Boston”] Using the leave-one-out cross-validation and 5-fold cross-validation techniques to compare the...
[USING R & dataset “Boston”] Using the leave-one-out cross-validation and 5-fold cross-validation techniques to compare the performance of models in (a) and (b) with: (a) SalesPredict <- lm(Sales ~ Price + Urban + US, data = Carseats) (b) SalesRevise <- lm(Sales ~ Price + US, data = Carseats) Hint: Functions update (with option subset) and predict.
Im trying to create a function in C where it takes an array and size and...
Im trying to create a function in C where it takes an array and size and returns the largest absolute value in the array (either negative or positive) using only stdio.h. Any help would be greatly appreciated! Ex: if the array is { -2.5, -10.1, 5.2, 7.0}, it should return -10.1 Ex: if the array is {5.1, 2.3, 4.9, 1.0}, it should return 5.1. double getMaxAbsolute(double array[], int size) {     double max = array[0];    double abs[size];    for...
Write a C++ code using while loop which is getting the two integers from the user,...
Write a C++ code using while loop which is getting the two integers from the user, and output how many numbers are multiples of 5, and how many numbers are multiples of 7 between the two integers (inclusive).
I was trying to figure out the flow of electrons in a battery connected to a...
I was trying to figure out the flow of electrons in a battery connected to a circuit. Conventionally, current is from the (+) terminal to the (-) terminal of the battery. Realistically it flows the other way round; from the (-) terminal to the (+) terminal. My question is, assuming electron flow is from the (-) terminal, would the battery's cathode be located at the (+) terminal and it's anode at the (-) terminal or would it be vice versa?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT