1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include<iostream>
#include<cmath>
#include<string>
using std::cout;
using std::cin;
using std::endl;
std::string number1 = "1234";
void count_up (int);
void count_down(int);
void count_between(int,int);
int main () {
int number;
int number2;
int y_n;
do {
cout << "Please enter one positive int. (int 1)" << endl;
cin >> number;
cout << "Please enter another positive int. (int 2)" << endl;
cin >> number2;
count_up(number);
cout << " is the count up from 0 to int 1." << endl;
cout << endl;
count_down(number);
cout << " is the countdown from int 1 to 0." << endl;
cout << endl;
count_between(number, number2);
cout << " is the count between int 1 and int 2." << endl;
cout << endl;
cout << "Do you want to run this again with 2 different ints? (1 f yes, 0 for no)" << endl;
cin >> y_n;
} while (y_n > 0);
return 0;
}
void count_up(int number) {
int x = 0;
while (x <= number)
{ cout << x << " ";
++x;
}
return;
}
void count_down (int number){
int x = 0;
while (number >= x)
{ cout << number << " ";
number--;
}
return;
}
void count_between (int number, int number2){
{
if(true)
{
while (number <= number2)
{
cout << number << " ";
number++;
}
return;
}
else
{
while (number2 <= number)
{
cout << number2 << " ";
number2++;
}
return;
}
}
}
|
Here is my code and now I am suppose to add a function to get a positive integer as a string, get_positive_int(),
and add error checking to your code above. If the user doesn't enter a positive
integer, they get another prompt and chance. They should get as many prompts and
chances as it takes.
You will need to #include <string>, and then you can create a string object (notice I said object, rather than variable), i.e. std::string number1 = “1234”;
Now, write a function, is_positive_int(), responsible for checking that the string entered by the user is a positive integer. It will return a 1, if the string is a positive number, and the function will return 0 otherwise. Hint: use the size() and at() functions associated with the string object. When we have an object, we can access attributes and actions associated with it by using the dot operator, ., between the object and the function. For example, number1.size() returns the number of characters in the string, and number1.at(0) returns the character at the starting position in the string. Therefore if we assign the string object number1 = “1234”;, then number1.size() returns 4 and you can access characters at positions 0 – 3.
If the user didn’t enter a positive integer, you will call get_ positive_int() to reprompt the user for a new number. You can either pass the string object as a reference or you can declare a local string object and return the value of the object. When the user has entered an acceptable positive integer, then you can use a C function called atoi(), which is in #include <cstdlib>, to convert the string into an integer. The problem with using a C function is that C is not a OOP language, and therefore, it doesn’t support objects This means that we have to take the C++ string object and turn it into a C string to pass to atoi() as an argument. Use the c_str() function associated with the string object to convert the string to a C-style string, i.e. atoi(number1.c_str()).
I can not figure this out. please someone help me. Thank you.