Trying something basic and need help
Moderator: Coders of Rage
- Falco Girgis
- 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
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
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.
Re: Trying something basic and need help
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:
The errors:
and if i take "stdafx.h" out like you guys have said in the past i get this:
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;
}
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 ==========
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Trying something basic and need help
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.
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.
Re: Trying something basic and need help
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.
- dandymcgee
- 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
That's one Nice piece of codeMarauderIIC wrote:Although if you leave it included long enough, the magic numbers (122, 42, 32, 32, 0) will eventually disappear.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);
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.
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!
Re: Trying something basic and need help
Thanks dandy
Now when i take marauders code and advise these are the errors i get:
My new code:
The Errors:
Now im even more confused than the first errors.
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;
}
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 ==========
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Trying something basic and need help
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.
Re: Trying something basic and need help
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.
- Ginto8
- 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
Simple. Take this:Aeolus wrote:Then how do i get it to print out the sums?
Code: Select all
cout << "the sum is: " << atoi(number1.c_str()) + atoi(number2.c_str());
Code: Select all
cout << "the sum is: " << number1 + number2;
Code: Select all
cout << "the sum is: " << (number1 + number2);
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.
Re: Trying something basic and need help
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:
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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Trying something basic and need help
cin >> SOME TEMP VARIABLE; (number1, number2, name)...
or the bad practice non portable system("pause");
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.
- LeonBlade
- 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
Use getch() don't know what header lol... conio?
I haven't used C++ in forever
I haven't used C++ in forever
There's no place like ~/
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Trying something basic and need help
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.
- LeonBlade
- 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
Yeah, but that's for debug... use getch()
And I was wrong it's curses.h lol
And I was wrong it's curses.h lol
There's no place like ~/