Hi all,
I'm beginner in c++, I'm trying practice on c++ basics, since few minutes I tried practice "setw" operator, I wrote this trivial program to print some data in console screen, but when I tried build it it gave me error..!

//print game names and my prefer rating in table
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	char g01='Excellent';
	char g02='Good';
	char g03='Bad';
	
	cout << "Game:          " << setw(10) << "My Rate" << endl
		 << "Hitman 4       " << setw(10) << g01 << endl
		 << "Infernal       " << setw(10) << g02 << endl
		 << "Harry potter 2 " << setw(10) << g03 << endl;
	return 0;
}
------ Build started: Project: iammfa07_setw, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(7) : error C2015: too many characters in constant
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(8) : warning C4305: 'initializing' : truncation from 'int' to 'char'
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(8) : warning C4309: 'initializing' : truncation of constant value
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(9) : warning C4305: 'initializing' : truncation from 'int' to 'char'
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(9) : warning C4309: 'initializing' : truncation of constant value
Build log was saved at "file://c:\Users\iammfa\Documents\Visual Studio 2008\Projects\iammfa07_setw\Debug\BuildLog.htm"
iammfa07_setw - 1 error(s), 4 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

can someone help beginner before despair..:(
regards
iammfa

Recommended Answers

All 8 Replies

String literals use double quotes, character literals use single quotes. My guess is that you need to come to terms with the fact that char is not a string type, then look at the std::string class.

The errors don't appear to be related to your use of "setw()".

The numbers in parentheses after the file path and name are line numbers. Double-click on one of them to go to the line where the error is indicated. Always fix the first error listed first, it may be causing the others.

In your case the first error is a dataType/storage error. A char object/variable is only able to hold one (1) single character, such as 'A'. It can not hold an entire string such as "Excellent".

If you want to use the phrases "Excellent", "Good", and "Bad", you will need to use string objects.

#include <iostream>
#include <iomanip>
[b]#include <string>[/b]
using namespace std;
int main()
{
	[b]string[/b] g01='Excellent';
	[b]string[/b] g02='Good';
	[b]string[/b] g03='Bad';
	
	cout << "Game:          " << setw(10) << "My Rate" << endl
		 << "Hitman 4       " << setw(10) << g01 << endl
		 << "Infernal       " << setw(10) << g02 << endl
		 << "Harry potter 2 " << setw(10) << g03 << endl;
	return 0;
}

Thanks Fbody for trying help me, I paste your corrected code but compiler gave me error:

------ Build started: Project: iammfa07_setw, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(8) : error C2015: too many characters in constant
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(9) : error C2440: 'initializing' : cannot convert from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        No constructor could take the source type, or constructor overload resolution was ambiguous
c:\users\iammfa\documents\visual studio 2008\projects\iammfa07_setw\main.cpp(10) : error C2440: 'initializing' : cannot convert from 'int' to 'std::basic_string<_Elem,_Traits,_Ax>'
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Ax=std::allocator<char>
        ]
        No constructor could take the source type, or constructor overload resolution was ambiguous
Build log was saved at "file://c:\Users\iammfa\Documents\Visual Studio 2008\Projects\iammfa07_setw\Debug\BuildLog.htm"
iammfa07_setw - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Sorry, just copied and pasted your o/p. Change the single quotes to double quotes and see if it makes a difference...

Thanks Fbody for trying help me, I paste your corrected code but compiler gave me error:

Proof positive -- if you give an answer to someone, they will just copy and paste your solution into their code rather than try to understand what you wrote and fix their code.

iammfa (and others), use your brain, not ours.

Proof positive -- if you give an answer to someone, they will just copy and paste your solution into their code rather than try to understand what you wrote and fix their code.
...

:$ I thought about that, a little too late though.

Thanks All for helping me ..

In YouTube c++ tutorial I saw person using "Code::Blocks" IDE when programming with SDL lib, What are the additional features, which "Code::Blocks" IDE gives me compared with C++ 2008 express IDE ..?

Thanks All for helping me ..

In YouTube c++ tutorial I saw person using "Code::Blocks" IDE when programming with SDL lib, What are the additional features, which "Code::Blocks" IDE gives me compared with C++ 2008 express IDE ..?

Nothing worth worrying about. You still have to write the code yourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.