Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Leadorn

Humm I thought i could program but what is wrong

This topic is 5963 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


  
#include <iostream.h>
#include <stdlib.h>

int main()
{
 char input[10];
 int num;
 cin >> input;


 for(int i =0;i <= sizeof(input); i++)
 {
         if (input[i] == '*') num+=1;
 }


 cout << num;



      system("PAUSE");
      return 0;
}
  
The code are going to count the number of * in the char array. Edited by - leadorn on March 21, 2002 7:49:27 AM Edited by - leadorn on March 21, 2002 8:09:28 AM

Share this post


Link to post
Share on other sites
Advertisement
ok, first off- im pretty sure you want to use
strlen() and not sizeof() in that for loop..
but then again i have no idea what this little
code block is supposed to do so i could be wrong (but i doubt it)

next up- you say you're trying to count the number
of *'s in the char array.. but you're trying to compare
apples and oranges here..
since you're looping here, why dont you go ahead and
change your for include this:

    
if (input[i] == '*') num+=1; // will check i's char in the array


//instead of


if (input == '*') num+=1; // causes an error. cannot compare

// input[10] with a single char


-eldee
;another space monkey;
[ Forced Evolution Studios ]

[edit: forum stole my goddamn array brackets]

[edited by - eldee on March 21, 2002 8:01:28 AM]

Share this post


Link to post
Share on other sites
Hello

I dont know why there isent an input == ''*''


I know now what is wrong.

I used too small char arry. The for funktion goes to 11. end of array.

Share this post


Link to post
Share on other sites
When you declare a variable, you need to initialize to something, otherwise it will have some random value.

int num = 0;

Share this post


Link to post
Share on other sites
that the for goes to 11 is ok, but you have maybe forgotten, that a terminating ''0'' is applied ?!?

Share this post


Link to post
Share on other sites
A few points:

- Why are you using iostream.h instead of iostream?
- Why are you using char arrays rather than "proper" strings?
- If you want to count something, why aren''t you using the count algorithm?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi!

Actually all previous posters are right, as you have made several minor mistakes in your code. First of all you should include string.h into your code. Then you should initialize the variable num to zero: int num=0; Then instead of using sizeof(input), you should use strlen(input) which is included in the header string.h. sizeof will always return how many bytes are allocated, but what you want is, how many characters there are in the string. And this is determined with the terminator character ''\0''. So all strings have ''\0'' at the end. And your cin >> input does that too. But be careful: if you allocate 10 bytes (with char input[10]) you can actually use only 9 for the string - the last one is the terminator ''\0''. And also in your loop: if you allocate 10 bytes, you can use only 10 bytes, but your code counts from 0 to sizeof(line) ie. 10, so you''re actually addressing a memory allocation that is out of bounds (input[10]) and this could lead to a crash. So instead of using

for (int i=0;i<=strlen(input);i++)

use:

for (int i=0;i<strlen(input);i++)

OK, hope it helps

Happy coding

Firestar

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
First of all you should include string.h into your code.

Nooooo! He''s using C++, not C. Therefore, include the "string" (no ".h") header and use std::string.

Share this post


Link to post
Share on other sites
quote:
Original post by atcdevil
When you declare a variable, you need to initialize to something, otherwise it will have some random value.

int num = 0;


thats totaly wrong..
when you initialize an int, it sets it to zero.

Share this post


Link to post
Share on other sites

This topic is 5963 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.

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!