Whats wrong with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
#include <string>

int main()
{
	string spacer = std::string s( 9, ' ' );        ;
	string ruler;
	
	
	cout << setw(9) << "1" << setw(9) <<" 2"<< setw<< "3"<< "\n\n";
	return 0;

}
@Bolong Yu

Here is the program, working, with explanations on why yours was having problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// spacer string.cpp : main project file.
#include <iostream>
#include <iomanip> // Need for setw() 
#include <string>

using namespace std;

int main()
{
	string spacer(9,' '); // Set string spacer to 9 spaces, but not used
	string ruler; // Created, but never used
	
	cout << spacer << "1" << spacer <<" 2"<< spacer << "3"<< "\n\n"; // Using variable, spacer
	cout << setw(9) << "1" << setw(9) <<" 2"<< setw(9)<< "3"<< "\n\n";// Forgot the '(9)' on last setw
	return 0;
}
**DISREGARD**

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

int main()
{
	std::string spacer;
	spacer="         ";
	
	std::cout<<spacer<<"1"<<spacer<<"2"<<spacer<<"3";
}


What I think you are trying to do is space out the numbers by 9 whitespaces. First of all, there is no use for #include <string> because you are not using functions defined from string header. Next, if you want a string with 9 spaces, you do not do (9,' '), you either define std::string spacer to be " " or create a for loop like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	char spacer[77];
	for(int i=0;i<9;i++)
	{
		strcat(spacer," ");
	}
	
	std::cout<<spacer<<"1"<<spacer<<"2"<<spacer<<"3";
}


In this case, we are using the strcat function, so we have to include <string.h>.

Finally, you are not using the variable s, spacer, or ruler. When you define a variable and don't use it, it is like throwing it away and letting it rot.

EDIT: whitenite is using <iomanip>, which you must define if you want to use the functions. Also, he agrees that you are throwing away variables.

**DISREGARD**
Last edited on
What I think you are trying to do is space out the numbers by 9 whitespaces. First of all, there is no use for #include <string> because you are not using functions defined from string header

Der. He was using std::string which, surprisingly, is defined in the <string> header. But using unsafe C functions is much better. It's a good thing you chimed in to show him the error of his ways.

Next, if you want a string with 9 spaces, you do not do (9,' '), you either define std::string spacer to be " " or create a for loop like this:

If you want a string with 9 spaces, using that string constructor is certainly a valid way to do it, and certainly far better than your for loop for conciseness, readability, safety and efficiency.

Sorry, cire.

cire wrote:
Der. He was using std::string which, surprisingly, is defined in the <string> header. But using unsafe C functions is much better. It's a good thing you chimed in to show him the error of his ways.


strcat(which is defined in string.h) is unsafe? Did not know that.

cire wrote:
If you want a string with 9 spaces, using that string constructor is certainly a valid way to do it, and certainly far better than your for loop for conciseness, readability, safety and efficiency.

I wasn't sure if that was defined as a function and he was writing it wrong or something.

Again, sorry and thank you for the post (learned something new).
You the best cire!
Last edited on
Topic archived. No new replies allowed.