c++ code help

Hi, I have a problem with the spacing for my output.

**Here is my coding:

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
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
	int lower, upper, count = 0;
	cout << "Enter lower and upper values";
	scanf("%d", &lower);
	scanf("%d", &upper);
	//this loop will continue till user enters a valid input
	while (lower < 32 || upper > 126 || lower > upper) {
		cout << "\nValues must be in range 32 to 126 inclusive";
		cout << "\nEnter lower and upper values";
		//read data again
		scanf("%d", &lower);
		scanf("%d", &upper);
	}
	cout << "\nCharacters for ASCII values between " << lower << " and " << upper;
	cout << "\n----+----+----+-\n";//prints the line
	for (; lower <= upper; lower++) {
		//print a character
		printf("%c", lower);
		count++;
		//check if we have already printed 16 characters
		if (count == 16) {
			cout << endl;
			count = 0;
		}
	}
	cout << "\n----+----+----+-\n";//prints the lines

	return 0;
}


**When I input these values: 90 121
Below is my ouput, but is is not correct because it has that big space between the characters and the line.

Enter lower and upper values
Characters for ASCII values between 90 and 121
----+----+----+-
Z[\]^_`abcdefghi
jklmnopqrstuvwxy
                 //<--this the big space that is making my coding incorrect
----+----+----+-


**Below is what my ouput should look like:
Enter lower and upper values
Characters for ASCII values between 90 and 121
----+----+----+-
Z[\]^_`abcdefghi
jklmnopqrstuvwxy
----+----+----+-


**If you could fix my coding so my output is correct, thanks.

[/code]
Last edited on
Hello bal160730,

In line 29 try removing the first "\n" and see what happens.

I have to ask why the use of "scanf" and "printf" in a C++ program?

Hope that helps,

Andy
Topic archived. No new replies allowed.