Trying something basic and need help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Trying something basic and need help

Post 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).
User avatar
Aeolus
Chaos Rift Regular
Chaos Rift Regular
Posts: 179
Joined: Fri Jan 16, 2009 2:28 am

Re: Trying something basic and need help

Post by Aeolus »

Just finished 15. Finally caught up! Make 16 now haha. But anyways, omg marcel had to drink that thing. I almost puked.
Hyde from That 70's Show wrote:Woman are like muffins... Once you have a muffin you will do anything to have another muffin... And they know that.
User avatar
Aeolus
Chaos Rift Regular
Chaos Rift Regular
Posts: 179
Joined: Fri Jan 16, 2009 2:28 am

Re: Trying something basic and need help

Post 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 ==========
Hyde from That 70's Show wrote:Woman are like muffins... Once you have a muffin you will do anything to have another muffin... And they know that.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Trying something basic and need help

Post 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?
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Aeolus
Chaos Rift Regular
Chaos Rift Regular
Posts: 179
Joined: Fri Jan 16, 2009 2:28 am

Re: Trying something basic and need help

Post by Aeolus »

Im using visual c++ 2008 express edition and i cant figure out how to turn that precompiled headers thing off. Its annoying
Hyde from That 70's Show wrote:Woman are like muffins... Once you have a muffin you will do anything to have another muffin... And they know that.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Trying something basic and need help

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Aeolus
Chaos Rift Regular
Chaos Rift Regular
Posts: 179
Joined: Fri Jan 16, 2009 2:28 am

Re: Trying something basic and need help

Post 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.
Hyde from That 70's Show wrote:Woman are like muffins... Once you have a muffin you will do anything to have another muffin... And they know that.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Trying something basic and need help

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Aeolus
Chaos Rift Regular
Chaos Rift Regular
Posts: 179
Joined: Fri Jan 16, 2009 2:28 am

Re: Trying something basic and need help

Post by Aeolus »

Then how do i get it to print out the sums?
Hyde from That 70's Show wrote:Woman are like muffins... Once you have a muffin you will do anything to have another muffin... And they know that.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Trying something basic and need help

Post 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).
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Aeolus
Chaos Rift Regular
Chaos Rift Regular
Posts: 179
Joined: Fri Jan 16, 2009 2:28 am

Re: Trying something basic and need help

Post 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;
}
Hyde from That 70's Show wrote:Woman are like muffins... Once you have a muffin you will do anything to have another muffin... And they know that.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Trying something basic and need help

Post by MarauderIIC »

cin >> SOME TEMP VARIABLE; (number1, number2, name)...
or the bad practice non portable system("pause");
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Trying something basic and need help

Post by LeonBlade »

Use getch() don't know what header lol... conio?
I haven't used C++ in forever
There's no place like ~/
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Trying something basic and need help

Post by MarauderIIC »

Just force a push enter during debug.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Trying something basic and need help

Post by LeonBlade »

Yeah, but that's for debug... use getch()
And I was wrong it's curses.h lol
There's no place like ~/
Post Reply