Removing the last period or decimal after the final number

Sep 17, 2021 at 4:21pm
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.

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
#include <iostream>
using namespace std;
int main (){
	
	int num1;
	int num2;
	
	cout << " ";
    cin >> num1;

    cout << " ";
    cin >> num2;
	
	int counter = num1;
	if (num1 == num2){
		cout << "EQUAL";
	}
	else if (num1 >= num2){
		int counter = num1;
		while (counter >= num2){
			cout << counter << ".";
			counter--;
		}
		
	}
	else if (num1 <= num2){
		while (counter <= num2){
			cout << counter << ".";
			counter++;
		}
	}
	return 0;
}
Last edited on Sep 18, 2021 at 4:01am
Sep 17, 2021 at 4:47pm
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

Sep 18, 2021 at 4:02am
Hi thanks for replying to my problem, 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.
Sep 18, 2021 at 5:32am
Consider:

    4.5.6.7.8.9.10.11.12.13

is the same as:

    4.5.6.7.8.9.10.11.12.

with a '13' on the end.


That should help. If if doesn't, you need to think about it more. (Until it does help.)

Good luck!
Last edited on Sep 18, 2021 at 5:33am
Sep 18, 2021 at 9:11am
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

Sep 18, 2021 at 11:15am
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.

*https://en.wikipedia.org/wiki/Code_refactoring

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
#include <iostream>
#include <limits>
#include <sstream>


int GetInt(const std::string& prompt,
           const int min = std::numeric_limits<int>::min(),
           const int max = std::numeric_limits<int>::max())
{
    int n{};
    while (!(std::cout << prompt && std::cin >> n) ||
          (n < min || n > max))
    {
        std::cout << "Invalid input." << '\n';
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    return n;
}

int main()
{
    int first = { GetInt("First number: ") };
    int last  = { GetInt("Last number:  ") };

    std::stringstream ss{};
    if (first == last)
    {
        ss << "EQUAL";
    }
    else
    {
        auto step = (first > last) ?  -1 : 1;
        for (auto number{ first } ; number != last; number += step)
        {
            ss << number << ".";
        }
        ss << last;
    }

    std::cout << ss.str() << '\n';
}


First number: 1
Last number:  1
EQUAL


First number: 1
Last number:  5
1.2.3.4.5


First number: 5
Last number:  1
5.4.3.2.1


First number: d
Invalid input.
First number: 1
Last number:  5
1.2.3.4.5
Sep 18, 2021 at 2:04pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
   std::cout << "Enter the starting number: ";
   int first { };
   std::cin >> first;

   std::cout << "Enter the ending number: ";
   int last { };
   std::cin >> last;

   std::cout << '\n';

   for (int itr { first }; itr < last; ++itr) { std::cout << itr << '.'; }

   std::cout << last << '\n';
}

Enter the starting number: 2
Enter the ending number: 15

2.3.4.5.6.7.8.9.10.11.12.13.14.15
Sep 19, 2021 at 1:12am
Well, since we're posting solutions...

1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
  int n, N;
  std::cin >> n >> N;
  std::cout << n;
  while (n++ < N) std::cout << "." << n;
}

Make sure you follow the requirement that n <= N or you'll be watching numbers print for a day or two.
Sep 19, 2021 at 1:42pm
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.
Topic archived. No new replies allowed.