QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

Game help

This is a discussion on Game help within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; I have started learnign c++ and i know i do have a logn way to go. But i decided to ...

Reply
 
LinkBack Thread Tools
Old 07-10-2007, 11:42 PM   #1
 
altunozara's Avatar
 
Join Date: Feb 2006
Location: www.ultimatetalkforums.com Games, RPG, quickchat and more!
Posts: 531
Trader Feedback: 0
Default Game help

I have started learnign c++ and i know i do have a logn way to go. But i decided to make lots of mini tools in the process just to help me learn. In the future i will make them all fun in some way. i have been trying to make a program that asks for your name and you input your name and it sais it back

#include <iostream>
using namespace std;
int main()
{
int name;

cout<<"Hello! What is your name?: ";
cin>> name;
cin.ignore();
cout<<"Hello "<< name <<"\n";
cin.get();
}


that is what i have so far but it doesnt work please tell me what i did wrong...
__________________
[CENTER][COLOR=black][COLOR=black][URL="http://forums.qj.net/f-psp-firmware-discussion-253/t-psp-firmware-30-get-it-58053.html"][COLOR=cyan]would you upgrade?[/COLOR][/URL] [/COLOR][FONT=Times New Roman][COLOR=navy][URL="http://www.mindistortion.net/iwantyoursoul/?i_am=altunozara"]OMG ITS REAL![/URL][/COLOR][/FONT][COLOR=black] [URL="http://forums.qj.net/showthread.php?t=61756&page=1&pp=10"][COLOR=lime]Sony vs Nintendo[/COLOR][/URL][/COLOR][/COLOR][/CENTER]
[CENTER] [/CENTER]
[CENTER][URL="http://www.ultimatetalkforums.com"][IMG]http://i94.photobucket.com/albums/l82/Altunozara/userbar2.jpg[/IMG][/URL][/CENTER]
[CENTER][SIZE=3]i [/SIZE][URL="http://forums.qj.net/www.gamingwell.com"][SIZE=3]wonder what could be under this hyperlink..hmm[/SIZE][/URL][/CENTER]
[CENTER][SIZE=3][COLOR=green][B][I]Maniakc is awsome!!![/I][/B][/COLOR][/SIZE][/CENTER]
[CENTER][FONT=Arial Black][SIZE=3][COLOR=#660099][I][U][URL="http://forums.qj.net/member.php?u=58299"]The Brain Pwns!!![/URL][/U][/I][/COLOR][/SIZE][/FONT][/CENTER]

[COLOR=sienna]Creator of:[/COLOR][URL="http://forums.qj.net/f-psp-development-forum-11/t-release-go-go-go-beta-66652.html"][COLOR=sienna]GO GO GO![/COLOR][/URL]
altunozara is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 01:50 AM   #2
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

'int' is short for 'integer' meaning number, name isnt a number, change:
Code:
int name;
to:
Code:
char name[20];
(assuming the maximum name length is 20 characters).

I just tested it, that was your only problem, it should work now.
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 02:00 AM   #3

is not posting very often
 
Glynnder's Avatar
 
Join Date: Feb 2006
Location: omnipresent
Posts: 5,161
Trader Feedback: 0
Default

what about setting name to be a string?
__________________
Quote:
Originally Posted by Abe
Either way, if you don't know, don't guess. Stick to answering questions about stuff you're qualified to answer, like Pokemon questions or something along those lines.
http://forums.qj.net/501501-post26.html
Glynnder is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 02:06 AM   #4

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Quote:
Originally Posted by altunozara
I have started learnign c++ and i know i do have a logn way to go. But i decided to make lots of mini tools in the process just to help me learn. In the future i will make them all fun in some way. i have been trying to make a program that asks for your name and you input your name and it sais it back

#include <iostream>
using namespace std;
int main()
{
int name;

cout<<"Hello! What is your name?: ";
cin>> name;
cin.ignore();
cout<<"Hello "<< name <<"\n";
cin.get();
}


that is what i have so far but it doesnt work please tell me what i did wrong...
That is because it is expecting an integer and not a string.
Read: http://www.nicollet.net/meta/tut.cpp
-= Double Post =-
Do not follow Jaso_psp's advice, he is giving you C advice rather then C++.
Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;
	cout <<"Hello! What is your name?: ";
	cin >> name;
	cout << "Hello "<< name << endl;
	cin.get(); 
}

Last edited by yaustar; 07-11-2007 at 02:09 AM.. Reason: Automerged Doublepost
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 08:45 AM   #5
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

oh crap, i gave him C advice for another thread aswell :S. Then again, whats so bad about my method, it outputs the same result.
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 08:46 AM   #6

It's good to be free...
 
Archaemic's Avatar
 
Join Date: Feb 2007
Posts: 2,440
Trader Feedback: 0
Default

It's a C string. If you're in C++, you should use C++ strings.
__________________
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
Archaemic is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:48 AM   #7
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

I think its better to use char* or char[] so that way u don't have to include iostream (it adds a lot of unneeded stuff to the eboot or executable)
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:49 AM   #8

It's good to be free...
 
Archaemic's Avatar
 
Join Date: Feb 2007
Posts: 2,440
Trader Feedback: 0
Default

Um, strings are in the string header, not iostream. iostream you don't need anyway unless you're doing file IO.
__________________
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
Archaemic is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:50 AM   #9
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

C version:
char *name = (char*)malloc(sizeof(char )*20);

C++ version:
char *name = new char[20];

or just do it with arrays like Jaso said
char name[20];
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:52 AM   #10

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

It only links what is needed and quite frankly, who cares if there is another 2k on the ELF size? If you are going to use C++ then make full use of the C++ standard library. That includes type safety and self sufficient objects (i.e. strings, vectors, lists). With C++ strings, you don't have to worry about NULL terminated strings or accidentally overrunning the char array. It is all handled internally which leaves the programmer to concentrate on the functionality of the program instead.

Use char * is just asking for trouble.
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:53 AM   #11
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

Quote:
Originally Posted by Archaemic
Um, strings are in the string header, not iostream. iostream you don't need anyway unless you're doing file IO.
i still don't like using them
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:55 AM   #12

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Quote:
Originally Posted by pspballer07
C version:
char *name = (char*)malloc(sizeof(char )*20);

C++ version:
char *name = new char[20];

or just do it with arrays like Jaso said
char name[20];
That is the worse advice I have seen. Apart from the last one, the first two leads to memory fragmentation and memory leaks if not handled correctly or used repeatedly.

The C++ version is:
Code:
std::string name = "I really don't give a crap what size this string is";
name = "nor do I have to bother dealing resizing or having to use the dangerous C string library to do this";
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:58 AM   #13
MiG
 
MiG's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 499
Trader Feedback: 0
Default

Quote:
Originally Posted by pspballer07
i still don't like using them
Cry me a river.. C++ Strings are just better, listen to yaustar.
MiG is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:58 AM   #14

It's good to be free...
 
Archaemic's Avatar
 
Join Date: Feb 2007
Posts: 2,440
Trader Feedback: 0
Default

yaustar: If you don't strip the executable properly (which the PSP toolchain does anyway, so it doesn't matter), iostream can add upwards of 230kB to a file, IIRC.

Wow, I'm wrong. I just tested it in Cygwin with a basically empty file (not using the PSP toolchain, but whatever), and the file is over 400kB before stripping. It's 230kB AFTER stripping. That's a big deal compared to what you can usually pack into an executable.
__________________
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
Archaemic is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:58 AM   #15

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

This code is quite easily done by a beginner following your advice:
Code:
char *name1 = (char*)malloc(sizeof(char )*20);
// Put a string in name1
char *name2 = (char*)malloc(sizeof(char )*20);
// Put a string in name2

// Programmer attempts to make name2 string equal to name1
name1 = name2;
// Whoops, memory leak through pointer reassignment
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:59 AM   #16

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Quote:
Originally Posted by Archaemic
yaustar: If you don't strip the executable properly (which the PSP toolchain does anyway, so it doesn't matter), iostream can add upwards of 230kB to a file, IIRC.

Wow, I'm wrong. I just tested it in Cygwin with a basically empty file (not using the PSP toolchain, but whatever), and the file is over 400kB before stripping. It's 230kB AFTER stripping. That's a big deal.
It is barely 6k on a PC. I am not with my PSP toolchain at the moment so I try it later but I doubt it be THAT large.
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 10:59 AM   #17
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

Quote:
Originally Posted by yaustar
That is the worse advice I have seen. Apart from the last one, the first two leads to memory fragmentation and memory leaks if not handled correctly or used repeatedly.

The C++ version is:
Code:
std::string name = "I really don't give a crap what size this string is";
name = "nor do I have to bother dealing resizing or having to use the dangerous C string library to do this";
that is why I handle it and free() it or delete it. using char* "in my opinion" is better
-= Double Post =-
Quote:
Originally Posted by yaustar
This code is quite easily done by a beginner following your advice:
Code:
char *name1 = (char*)malloc(sizeof(char )*20);
// Put a string in name1
char *name2 = (char*)malloc(sizeof(char )*20);
// Put a string in name2

// Programmer attempts to make name2 string equal to name1
name1 = name2;
// Whoops, memory leak through pointer reassignment
well if the beginner read up on it instead of jumping in then they would realize that u simply use strcpy()

Last edited by pspballer07; 07-11-2007 at 11:02 AM.. Reason: Automerged Doublepost
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:01 AM   #18

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Quote:
Originally Posted by pspballer07
that is why I handle it and free() it or delete it. using char* "in my opinion" is better
-= Double Post =-

well if u read up on it then u would realize that u simply use strcpy()
Convince me.

Even strcpy is dangerous:
Code:
char name1[10] = "";
char name2[30] = "12345678901234567890123465789";
strcpy( name1, name2 );
// Whoops CRASH!
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:03 AM   #19

It's good to be free...
 
Archaemic's Avatar
 
Join Date: Feb 2007
Posts: 2,440
Trader Feedback: 0
Default

Quote:
Originally Posted by yaustar
It is barely 6k on a PC. I am not with my PSP toolchain at the moment so I try it later but I doubt it be THAT large.
I'd like to know what compiler/strip utility YOU'RE using. I just repeated the test with MinGW and got the same results.

E] Yes, strcpy is very dangerous. It can lead to buffer overflows so quickly if you don't properly null-terminate a string, it's ridiculous. Even strncpy is dangerous if you don't know what you're doing.
__________________
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
Archaemic is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:04 AM   #20
 
Darkhunter's Avatar
 
Join Date: May 2006
Location: Quebec, Canada
Posts: 476
Trader Feedback: 0
Default

This is my code.

Spoiler for my code:
Code:
#include <cstdlib>
#include <iostream>

 using namespace std;

 int main(int argc,  char *argv[])
{
           string yourName;

           cout << "Please type your name and pres enter: ";
           cin >> yourName;

          cout << endl << "Hello, " << yourName << endl << endl;

          cout << " Press c and then enter to continue...";

          char justWait;
          cin >> justWait;

          return (EXIT_SUCCESS);
}
__________________
[U][URL="http://www.speedtest.net"][/URL][URL="http://www.speedtest.net/"][IMG]http://img459.imageshack.us/img459/5021/darkhuntercopyrz9jx4.png[/IMG][/URL][/U]
Darkhunter is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:06 AM   #21

It's good to be free...
 
Archaemic's Avatar
 
Join Date: Feb 2007
Posts: 2,440
Trader Feedback: 0
Default

Replace cstdlib with string
__________________
pəʇuɒɹɓ ɹoɟ ɓuɪɥʇou əʞɒʇ
Archaemic is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:07 AM   #22
 
Darkhunter's Avatar
 
Join Date: May 2006
Location: Quebec, Canada
Posts: 476
Trader Feedback: 0
Default

Quote:
Originally Posted by Archaemic
Replace cstdlib with string
It still works
__________________
[U][URL="http://www.speedtest.net"][/URL][URL="http://www.speedtest.net/"][IMG]http://img459.imageshack.us/img459/5021/darkhuntercopyrz9jx4.png[/IMG][/URL][/U]
Darkhunter is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:08 AM   #23
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

Quote:
Originally Posted by yaustar
Convince me.

Even strcpy is dangerous:
Code:
char name1[10] = "";
char name2[30] = "12345678901234567890123465789";
strcpy( name1, name2 );
// Whoops CRASH!
like i said, if u read on it, u'll know not to do that
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:10 AM   #24

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Using MSVS 8 2005
Code:
#include <iostream>

using namespace std;

int main()
{
	cout << "Hello World" << endl;
}
On a release build for size comes out at 8k.
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:12 AM   #25
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

well, this is for altunozara if they want to use char* or char[]
http://www.cplusplus.com/reference/clibrary/cstring/
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:14 AM   #26

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

Quote:
Originally Posted by pspballer07
like i said, if u read on it, u'll know not to do that
Or, you can avoid the issue completely and use C++ strings and reduce the number of possible bugs in your program regarding strings to a VERY small number.
-= Double Post =-
Quote:
Originally Posted by pspballer07
well, this is for altunozara if they want to use char* or char[]
http://www.cplusplus.com/reference/clibrary/cstring/
altunozara: Ignore this advice, if you are using C++, there is no reason at all to use C style strings.

Last edited by yaustar; 07-11-2007 at 11:17 AM.. Reason: Automerged Doublepost
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:19 AM   #27
 
pspballer07's Avatar
 
Join Date: Feb 2007
Posts: 315
Trader Feedback: 0
Default

Quote:
Originally Posted by yaustar
altunozara: Ignore this advice, if you are using C++, there is no reason at all to use C style strings.
WOW lol "ignore this advice," lol nobody's right but u

altunozara, do what u want
pspballer07 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:23 AM   #28

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

No, everybody's right but you pspballer07.
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:24 AM   #29
 
 
My Mood: Bored
Join Date: Apr 2006
Location: England ~¦¦¦|+|¦¦¦~
Posts: 1,112
Trader Feedback: 0
Default

all he wanted to do is use chars or strings instead of integers, we showed him many ways of making his program WORK. You guys just went off on one lol.
__________________
...Just Returned To The Scene...
JaSo PsP is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 07-11-2007, 11:27 AM   #30

Developer
 
yaustar's Avatar
 
Join Date: Jun 2006
Location: UK
Posts: 2,317
Trader Feedback: 0
Default

The way I see it is that using C strings in a C++ program where C++ strings can be used is like drinking soup with a fork when you can easily use a spoon.
yaustar is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
game

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 11:27 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us