quick c++ question

Joined
Sep 15, 2003
Messages
839
Reaction score
0
I'm just learning c++ and am not understanding something. The program is supposed to display this.
Code:
Julie Andrews        Guinevere
Richard Burton     King Arthur
Robert Goulet     Sir Lancelot

but the text won't allign right. I'm only using the text formatting functions of iostream.h for this. My current code is this.
Code:
#include <iostream.h>

int main()
{
	cout.width(15);	cout.setf(ios::left);	cout << "Julie Andrews";
	cout.width(15);	cout.setf(ios::right);	cout << "Guinevere";
	cout << endl;
	cout.width(15);	cout.setf(ios::left);	cout << "Richard Burton";
	cout.width(15);	cout.setf(ios::right);	cout << "King Arthur";
	cout << endl;
	cout.width(15);	cout.setf(ios::left);	cout << "Robert Goulet";
	cout.width(15);	cout.setf(ios::right);	cout << "Lancelot";
	cout << endl;
	return(0);
}
If anyone knows why this is please respond.

EDIT: also the only things covered at this point in the book for formatting are cout.width and cout.setf
 
try


#include <iostream>


probably not your problem but it will help down the road anyways.

How is it aligning now?
 
Code:
--------------------Configuration: Review 12 - Win32 Debug--------------------
Compiling...
main.cpp
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(7) : error C2065: 'cout' : undeclared identifier
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(7) : error C2228: left of '.width' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(7) : error C2228: left of '.setf' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(7) : error C2653: 'ios' : is not a class or namespace name
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(7) : error C2065: 'left' : undeclared identifier
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(7) : error C2297: '<<' : illegal, right operand has type 'char [14]'
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(8) : error C2228: left of '.width' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(8) : error C2228: left of '.setf' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(8) : error C2653: 'ios' : is not a class or namespace name
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(8) : error C2065: 'right' : undeclared identifier
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(8) : error C2297: '<<' : illegal, right operand has type 'char [10]'
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(9) : error C2065: 'endl' : undeclared identifier
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(9) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(10) : error C2228: left of '.width' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(10) : error C2228: left of '.setf' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(10) : error C2653: 'ios' : is not a class or namespace name
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(10) : error C2297: '<<' : illegal, right operand has type 'char [15]'
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(11) : error C2228: left of '.width' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(11) : error C2228: left of '.setf' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(11) : error C2653: 'ios' : is not a class or namespace name
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(11) : error C2297: '<<' : illegal, right operand has type 'char [12]'
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(12) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(13) : error C2228: left of '.width' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(13) : error C2228: left of '.setf' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(13) : error C2653: 'ios' : is not a class or namespace name
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(13) : error C2297: '<<' : illegal, right operand has type 'char [14]'
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(14) : error C2228: left of '.width' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(14) : error C2228: left of '.setf' must have class/struct/union type
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(14) : error C2653: 'ios' : is not a class or namespace name
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(14) : error C2297: '<<' : illegal, right operand has type 'char [9]'
c:\documents and settings\jeff.family\my documents\visual studio projects\lvp\chapter 3\review 12\main.cpp(15) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
Error executing cl.exe.

Review 12.exe - 28 error(s), 3 warning(s)

:p
btw, I have MSVC++6(only 7 years old :D)
 
That's because in the iostream header the objects cout, ios and endl are declared in the std namespace.

Don't worry about what the meens, but basically you need to say that you want to use the set of functions declared there with this line:

using namespace std;

That's remove the errors, not sure about the alignment though.
 
Ok, that cracks me up. :p One post, of six dots.... And he has two warning points :E LOL.



Well, now that I think of it, I'm betting it was edited by a mod...

Still funny.
 
It's been a while since I've messed with the spacing of C++, but here's what I remember:

1. You have to have the iomanip.h header file to use the manipulation fxns i.e. setw() and the alignment fxns.
2. To set the width, the easiest way is to do the following:
cout<<setw(15)<<"This is my string"

So the easiest way for you to do it would be like:

#include <iostream.h>
#include <iomanip.h>

int main()
{
cout<<setiosflags(ios::fixed);
cout.setf(ios::left);
cout<<setw(15)<<"Julie Andrews";
cout.setf(ios::right);
cout<<setw(15)<<"Guinivere"<<endl;
cout.setf(ios::left);
cout<<setw(15)<<"Richard Burton";
cout.setf(ios::right);
cout<<setw(15)<<"King Arthur"<<endl;
cout.setf(ios::left);
cout<<setw(15)<<"Robert Goulet";
cout.setf(ios::right);
cout<<setw(15)<<"Lancelot"<<endl;

return 0;
}

That should work.
 
An extra consideration when you use GCC (might apply to other compilers too, I'm not sure though):

Make sure you do a cout.unsetf(ios::[previousalignment]) after displaying whatever you wanted aligned. Apparantly, GCC doesn't unset the flag bits used to align the string to whatever you set it to.
 
setw(x) in the iomanip header...

x can be predefined... or it can be an integer ... like 7.
 
Get a good beginners book, join a c++ forum, go through the book, ask questions. :)
 
SLH said:
That's because in the iostream header the objects cout, ios and endl are declared in the std namespace.

Don't worry about what the meens, but basically you need to say that you want to use the set of functions declared there with this line:

using namespace std;

That's remove the errors, not sure about the alignment though.

Yeah namespaces are a bitch unless someone told you about them. But don't

using namespace std;

unless you be lazy. Instead, use:

using std::cout;
using std::cin;
using std::endl;
etc.

Less overhead for compile-time, I believe. But it's mainly just good practice.
 
Hehe, i'm lazy.

Cheers for the heads up!
 
Merc248 said:
An extra consideration when you use GCC (might apply to other compilers too, I'm not sure though):

Make sure you do a cout.unsetf(ios::[previousalignment]) after displaying whatever you wanted aligned. Apparantly, GCC doesn't unset the flag bits used to align the string to whatever you set it to.

Dude, if you have an EXACT url for downloading GCC I would kiss you (metaphicaly). I have never been able to download loat it simply because I dont understand the layout for hte site.
 
Back
Top