Hi, we've been tasked to make a program that will print the first and final input from the user with period or decimal between each number. However I need to remove the last period or decimal point after the final number. Heres the sample input and supposed to be output of the code:
4 13
4.5.6.7.8.9.10.11.12.13
However with the current program there's a decimal point or period after 13.
print the first and final input from the user with period or decimal between each number.
Well if that's all then as below. However if this isn't what is wanted then you need to be more specific as to the requirement of the program.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
int main()
{
int fst {}, lst {};
for (int num {}; (std::cout << "Enter an integer number (0 to exit) :") && (std::cin >> num) && (num != 0); lst = num)
if (fst == 0)
fst = num;
std::cout << fst << '.' << lst << '\n';
}
Enter an integer number (0 to exit) :1
Enter an integer number (0 to exit) :2
Enter an integer number (0 to exit) :3
Enter an integer number (0 to exit) :4
Enter an integer number (0 to exit) :5
Enter an integer number (0 to exit) :0
1.5
OK. Treat the first output differently. If not the first number then output a '.'. Then output the number:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
int main()
{
int f {}, l {};
std::cout << "Enter first and last numbers: ";
std::cin >> f >> l;
for (int n = f; n <= l; std::cout << n++)
if (n != f)
std::cout << '.';
std::cout << '\n';
}
Enter first and last numbers: 4 13
4.5.6.7.8.9.10.11.12.13
shunin, you don't need to worry about the GetInt() in the following (it's from something I'm messing around with), get the input however you are happy with.
One thing I did want to say is that in your code you have two blocks of code that look very similar. With some small changes you could make one block do both jobs. I recommend that you look at the code bellow and then refactor* your code. A few small changes can simplify your code a bit...it's nearly there.
Thanks to everybody! I am learning so much from you guys and because of all these codes and tips you replied, i passed on the activity with a 100 mark.