Page 2 of 5

Re: Trying something basic and need help

Posted: Wed Jan 21, 2009 11:14 pm
by Falco Girgis
I still gotta see the renderer. This weekend is nothing but dev weekend for me. I've had my share of ladies, drinks, parties and excitement to last me awhile (a week).

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 2:58 am
by Aeolus
Just finished 15. Finally caught up! Make 16 now haha. But anyways, omg marcel had to drink that thing. I almost puked.

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 1:54 pm
by Aeolus
Sorry to double post but i have another problem with another code and i dont want to make a new thread.

In my C++ programming for the Absolute beginner book at the end of a chapter it gives challenges. Well im stuck on Challenge 3/5.

3. Write a program that asks users for their names, that greets them, and that asks them for two numbers then provides a sum of those two numbers.

My code:

Code: Select all

// Chapter 1 challenge 3.cpp : User Input

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

//Declare section
string name = ;
string number1 = ;
string number2 = ;

int main()
{
	cout<< "What is your name?\n\n";
	cin>>name;
	cout<< "\n\n Hello "<<name.c_str();
	cout<< " and welcome to hell.";
	cout<<"Please name a number between 1-100";
	cin>>number1;
	cout<<"Please name a number between 1-1,000,000";
	cin>>number2;
	cout<<"the sum of that number is: "<<number1.c_str() + number2.c_str();

	cin.get();
	return 0;
}
The errors:

Code: Select all

1>------ Build started: Project: Chapter 1 challenge 3, Configuration: Debug Win32 ------
1>Compiling...
1>Chapter 1 challenge 3.cpp
1>.\Chapter 1 challenge 3.cpp(10) : error C2059: syntax error : ';'
1>.\Chapter 1 challenge 3.cpp(11) : error C2059: syntax error : ';'
1>.\Chapter 1 challenge 3.cpp(12) : error C2059: syntax error : ';'
1>.\Chapter 1 challenge 3.cpp(24) : error C2110: '+' : cannot add two pointers
1>Build log was saved at "file://c:\Users\Kyle\Documents\Visual Studio 2008\Projects\Chapter 1 challenge 3\Chapter 1 challenge 3\Debug\BuildLog.htm"
1>Chapter 1 challenge 3 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
and if i take "stdafx.h" out like you guys have said in the past i get this:

Code: Select all

1>------ Build started: Project: Chapter 1 challenge 3, Configuration: Debug Win32 ------
1>Compiling...
1>Chapter 1 challenge 3.cpp
1>.\Chapter 1 challenge 3.cpp(4) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>.\Chapter 1 challenge 3.cpp(5) : warning C4627: '#include <string>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>.\Chapter 1 challenge 3.cpp(29) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
1>Build log was saved at "file://c:\Users\Kyle\Documents\Visual Studio 2008\Projects\Chapter 1 challenge 3\Chapter 1 challenge 3\Debug\BuildLog.htm"
1>Chapter 1 challenge 3 - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 2:27 pm
by MarauderIIC
Alright. Firstly, in your project settings somewhere, there's a "use precompiled headers" option. Make that into "don't use precompiled headers".
Solution is below, with explanations.

Code: Select all

// Chapter 1 challenge 3.cpp : User Input

//remove stdafx.h and change the compiler option under project settings somewhere, or leave it and #include it in EVERY .cpp file in your project
#include <iostream>
#include <string>

using namespace std;

//Declare section
string name = ""; //you need a default value here if you're using the assignment. Otherwise, just "string name"
int number1; //assuming the user will provide valid inputs, use ints (short for integers) for number1 and number2.
int number2;

int main()
{
	cout << "What is your name?\n\n";
	//put spaces between << >> and anything else. Otherwise you may get errors (<< and >> are also the bit shift operators)
	cin >> name; 
	//endl makes a new line as well as forces the things in the cout buffer to be written to the screen right NOW.
	//additionally, i think c++ strings can be output without the .c_str(), which is used to convert them to c-style strings
	cout << "\n\n Hello "<< name << " and welcome to hell." << endl;
	cout <<"Please name a number between 1-100";
	cin >> number1;
	cout << "Please name a number between 1-1,000,000";
	cin >> number2;
	//you can't add strings, you have to add integers or other numeric types
	cout << "the sum of that number is: "<< number1 + number2;

	//if you really want to use strings for your numbers, the above would be:
	//cout << "the sum is: " << atoi(number1.c_str()) + atoi(number2.c_str());
	//atoi converts arrays of chars (as c-style strings are) to integers
	cin.get();
	return 0;
}
The errors:

Code: Select all

1>------ Build started: Project: Chapter 1 challenge 3, Configuration: Debug Win32 ------
1>Compiling...
1>Chapter 1 challenge 3.cpp
1>.\Chapter 1 challenge 3.cpp(10) : error C2059: syntax error : ';' //you have an ; after a = without anything in the middle
1>.\Chapter 1 challenge 3.cpp(11) : error C2059: syntax error : ';'
1>.\Chapter 1 challenge 3.cpp(12) : error C2059: syntax error : ';'
1>.\Chapter 1 challenge 3.cpp(24) : error C2110: '+' : cannot add two pointers //c_str() converts strings to arrays, which are also regarded as pointers for complicated reasons
and if i take "stdafx.h" out...

Code: Select all

1>------ Build started: Project: Chapter 1 challenge 3, Configuration: Debug Win32 ------
1>Compiling...
1>Chapter 1 challenge 3.cpp
//"stdafx.h" IF YOU USE PRECOMPILED HEADERS, MUST be the first thing included in every .cpp file in your project.
1>.\Chapter 1 challenge 3.cpp(4) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
...1>.\Chapter 1 challenge 3.cpp(29) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 3:27 pm
by Aeolus
Im using visual c++ 2008 express edition and i cant figure out how to turn that precompiled headers thing off. Its annoying

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 3:41 pm
by dandymcgee
MarauderIIC wrote:

Code: Select all

	const Equipment& curEquip = Play.partySystem.getPartyMember(relativeCursor).equip;
	SpriteRenderer& spriteRenderer = SpriteRenderer::getInstance();

	if(curEquip.right != -1)
		spriteRenderer.draw(*ItemSys.itemList[curEquip.right].img, false, 340, 122, 42, 32, 32, 0);

	if(curEquip.left != -1)
		spriteRenderer.draw(*ItemSys.itemList[curEquip.left].img, false, 388, 122, 42, 32, 32, 0); 

	if(curEquip.armor != -1)
		spriteRenderer.draw(*ItemSys.itemList[curEquip.armor].img, false, 436, 122, 42, 32, 32, 0); 
Although if you leave it included long enough, the magic numbers (122, 42, 32, 32, 0) will eventually disappear.

Okay, I've brought that up four times in the last two days, I guess I better leave it be now, before he finds something to rag on me about.
That's one Nice piece of code :)

EDIT: To answer your question Aeolus:
MSDN wrote:By default, when new projects are created, they are set up to use precompiled headers. To disable precompiled header, select Properties from the Project menu. Expand the Configuration Properties node, then expand the C/C++ node, and select Precompiled Headers. From the dropdown list next to Create/Use Precompiled Header, select Not Using Precompiled Header. Press OK to save these changes. For more information on precompiled headers, see Creating Precompiled Header Files.

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 5:27 pm
by Aeolus
Thanks dandy :worship:

Now when i take marauders code and advise these are the errors i get:

My new code:

Code: Select all

// Chapter 1 challenge 3.cpp : User Input

//remove stdafx.h and change the compiler option under project settings somewhere, or leave it and #include it in EVERY .cpp file in your project
#include <iostream>
#include <string>

using namespace std;

//Declare section
string name; 
int number1; 
int number2;

int main()
{
   cout << "What is your name?\n\n";
   cin >> name;
   cout << "\n\n Hello "<< name << " and welcome to hell." << endl;
   cout <<"Please name a number between 1-100";
   cin >> number1;
   cout << "Please name a number between 1-1,000,000";
   cin >> number2;
   cout << "the sum is: " << atoi(number1.c_str()) + atoi(number2.c_str());
   cin.get();
   return 0;
}
The Errors:

Code: Select all

1>------ Build started: Project: Chapter 1 challenge 3, Configuration: Debug Win32 ------
1>Compiling...
1>Chapter 1 challenge 3.cpp
1>.\Chapter 1 challenge 3.cpp(23) : error C2039: 'c_str' : is not a member of 'System::Int32'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>.\Chapter 1 challenge 3.cpp(23) : error C2039: 'c_str' : is not a member of 'System::Int32'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>Build log was saved at "file://c:\Users\Kyle\Documents\Visual Studio 2008\Projects\Chapter 1 challenge 3\Chapter 1 challenge 3\Debug\BuildLog.htm"
1>Chapter 1 challenge 3 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Now im even more confused than the first errors.

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 6:00 pm
by MarauderIIC
That's because you can't use .c_str() except on strings (as per the comment) and I changed number1 and number2 to ints.

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 7:26 pm
by Aeolus
Then how do i get it to print out the sums?

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 8:04 pm
by Ginto8
Aeolus wrote:Then how do i get it to print out the sums?
Simple. Take this:

Code: Select all

cout << "the sum is: " << atoi(number1.c_str()) + atoi(number2.c_str());
and turn it into this:

Code: Select all

cout << "the sum is: " << number1 + number2;
though, if that doesn't work you may have to do this:

Code: Select all

cout << "the sum is: " << (number1 + number2);
you may or may not have to (I don't use VC++, so I don't know).

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 8:13 pm
by Aeolus
now that worked, But now when i get to the end of the program and choose the last number it doesnt display the sum. Well i mean it does but it closes right after it does.

My code now:

Code: Select all

// Chapter 1 challenge 3.cpp : User Input

//remove stdafx.h and change the compiler option under project settings somewhere, or leave it and #include it in EVERY .cpp file in your project
#include <iostream>
#include <string>

using namespace std;

//Declare section
string name; 
int number1; 
int number2;

int main()
{
   cout << "What is your name?\n\n";
   cin >> name;
   cout << "\n\n Hello "<< name << " and welcome to hell." << endl;
   cout <<"Please name a number between 1-100";
   cin >> number1;
   cout << "Please name a number between 1-1,000,000";
   cin >> number2;
   cout << "the sum is: " << number1 + number2;
   cin.get();
   return 0;
}

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 8:24 pm
by MarauderIIC
cin >> SOME TEMP VARIABLE; (number1, number2, name)...
or the bad practice non portable system("pause");

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 8:51 pm
by LeonBlade
Use getch() don't know what header lol... conio?
I haven't used C++ in forever

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 9:41 pm
by MarauderIIC
Just force a push enter during debug.

Re: Trying something basic and need help

Posted: Thu Jan 22, 2009 9:48 pm
by LeonBlade
Yeah, but that's for debug... use getch()
And I was wrong it's curses.h lol