Jump to content
  • Advertisement
Sign in to follow this  
Trycast

HELP! O___________________________________O

This topic is 3989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

zomg im having so many problems coz when i do this
#include<iostream>
using namespace std;
int main()
{
    char *pointer = 0;  //yeah its null, ill just call it 0 lol
    strcpy(pointer,"hello world"); 
    cout<<helloworld;
    return 0;
}

i get an error and idk how to fix it so help me plz thx u very much ^_^

Share this post


Link to post
Share on other sites
Advertisement
Well, exactly what is your compiler telling you? I find that 99.987% of errors are easily traced using the garbage that your IDE or compiler spits up at you.
But you cannot strcpy into a NULL pointer. It's like trying to fill air with water. You need a bucket first. Use std::string, or look up dynamic memory allocation. You can new the char array, then delete it when you are done, but that's a little complex for someone "hello world"ing. Go with std::string. Simple, easy, painless.

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

int main()
{
std::string hello; //this is a string
hello = "hello world";
std::cout<<helloworld<<std::endl;
return 0;
}




That should do it, might not compile, haven't tested it.

Share this post


Link to post
Share on other sites

#include<iostream>
using namespace std;
int main()
{
char *pointer = new char(12); //yeah its null, ill just call it 0 lol
strcpy(pointer,"hello world\0");
cout<<pointer;
delete [] pointer;
return 0;
}


Share this post


Link to post
Share on other sites
Fitfool:
Your code won't compile, new uses brackets ( [] ) not parenthesis ( () ). It should be

char* ptr = new char[12];


You delete it correctly though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmarobo
Well, exactly what is your compiler telling you? I find that 99.987% of errors are easily traced using the garbage that your IDE or compiler spits up at you.
But you cannot strcpy into a NULL pointer. It's like trying to fill air with water. You need a bucket first. Use std::string, or look up dynamic memory allocation. You can new the char array, then delete it when you are done, but that's a little complex for someone "hello world"ing. Go with std::string. Simple, easy, painless.
*** Source Snippet Removed ***

That should do it, might not compile, haven't tested it.


Your code has a few problems. First of all, the correct header is <string>, not <string.h>. Second of all, you should be initializing your string with a value. Declaring it and then assigning it a value one line later is kind of asinine. Most importantly, you are referencing and undeclared variable named helloworld on your second to last line.

But, really, all of this is overkill for hello world:

#include <iostream>

int main()
{
std::cout<<"Hello World!"<<std::endl;
return 0;
}






Original poster: In general, you want a specific topic name, so that someone can tell just by reading it whether or not they can help you. Also, internet lingo (zomg et al.) makes me want to gouge my eyes out, but I suppose that's more personal preference.

Share this post


Link to post
Share on other sites
Just use std::string. If you couldn't spot the error in that code straight away, and don't have any need to sacrifice convenience and safety for some other benefit, then you're better off with a proper string rather than an array of characters.

#include <string>
#include <iostream>

int main()
{
std::string foo = "Hello, ";
std::string bar = "world.";

std::cout << foo + bar << std::endl; // "Hello, world."
}


The above would be bloated by another dozen or more lines were you to use char* 'strings' and expect some similar level of safety (although, I guess, the above isn't perfect: I think foo + bar can technically throw an exception if there's not enough memory).

Also, your identifiers are messed up (you have a char* called pointer, and nothing called helloworld).

In future, you may also want to post up the actual error that gets reported, so that we can help if the problem isn't so clear. :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmarobo
Fitfool:
Your code won't compile, new uses brackets ( [] ) not parenthesis ( () ). It should be

char* ptr = new char[12];


You delete it correctly though.

It certainly will compile as it is a perfectly legal and well defined line of code. What it does, however, I will leave as an exercise to you to figure out. Makes using wrong parantheses, or even believing that you can't use wrong ones without compiler error, quite dangerous in this case.

Share this post


Link to post
Share on other sites
Ok, here's the deal - I'm pretty sure you're actually a duplicate of a banned member trying to mess with people, and I'll be forced to remove you unless you convince me otherwise, which will at a minimum involve the following:
  • Title your threads properly. Everyone is looking for help, and the very long "smiley" is unneccesary and annoying.

  • Put in some effort to type properly. Sentences begin with a capital letter and end with a period or other punctuation, and things like "coz", "plz" and "thx" belong (if anywhere) in sms messages rather than forum posts you've typed with a full keyboard, type proper words instead.

Share this post


Link to post
Share on other sites

This topic is 3989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!