Archived

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

Leadorn

Humm I thought i could program but what is wrong

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
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
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
eldee: yes, are you sure ??? in debug mode all data is set to something like 0xdfdfdfdf or similar, and in retail mode there is nothing done (but mostly windows gives you a block of memory already Zeroed) ...

Share this post


Link to post
Share on other sites
quote:
Original post by eldee
thats totaly wrong..

It''s not totally wrong! POD objects will be default initialised to an indeterminate value. An exception is when they are in the global scope, in which case they are zero initialised.

Share this post


Link to post
Share on other sites
oh my...

in debug mode, some compilers initialize variables to some value (usually zero)... in release mode there is NO initialization..

perhaps you confuse C/C++ with basic or sth?

cheers

-----------
my quote is under construction

Share this post


Link to post
Share on other sites
Hey eldee, Try it out, I just did and it outputted some really weird long number. If you don''t do this in your programs, you''ll definetely have problems.

Share this post


Link to post
Share on other sites
quote:
Original post by 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
OK, hope it helps

Happy coding

Firestar



Don''t tell me what I can do. I don''t have to user string class. I know what the fuck strlen is. but I was trying to find errors.
cout << sizeof(input) // Output 10.

And I know that It would be better if I use stdlen because it stops when I come to \0.

And what difference does it make if I use the 3 version of c++ ISO standard or the older ones. Im only testing. So I don''t care about iostream using name space std.

The fucking problem was the array was to small. I now it now. So that last part was the only thing you had to write. Im not that stupid.

Sorry. I get this angry sometimes. But I hate when someone thinks im a total looser.

Share this post


Link to post
Share on other sites
quote:
Original post by Leadorn
Sorry. I get this angry sometimes. But I hate when someone thinks im a total looser.

If you don''t want people to think you are a total loser then you shouldn''t react like one.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
By the way, sizeof(input) will return the correct value (and is much more effecient that calling strlen(input) every single loop). This only works though if the array was declared in the same function your using it (i.e. if you pass it by parameter this information is lost).

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


I''m with you SabreMan. I love the way that when everyone uses C techniques when they are using C++, they will just ignore all comments about using proper C++ code. For some reason, no-one can see the advantages in STL.

I reckon its because people like LaMothe don''t, so they think its bad.

Share this post


Link to post
Share on other sites
quote:
Original post by Leadorn
Don't tell me what I can do. I don't have to user string class. I know what the fuck strlen is.



YOU asked for help here. if you dont want to hear
what people have to say in responce to your posts, dont
bother posting to begin with.
he was offering a suggestion, not demanding you convert to
std::string. dont act like a loser and no one will treat you
like one.

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




[edited by - eldee on March 21, 2002 1:38:34 PM]

quote:
Original post by SabreMan
If you don't want people to think you are a total loser then you shouldn't react like one.



That's a great link! I should note that down for my forum-hopping experiances.


[edited by - lackluster on March 21, 2002 2:21:28 PM]

Share this post


Link to post
Share on other sites
bloody heck, the error is a memory violation caused by trying to access the 10th charactor in the array!
whats all this strlen crap about? its irrelevant!

the problem is you use a <= in your for loop not a <
This means it stops when it hits i==10, and doesn''t call for ''input[10]'' as the array only goes from [0]-[9].

sorry to be so rude but seeing stupid things like ''why are you using iostream.h instead of iostream'' really is irritating!

Share this post


Link to post
Share on other sites
although i dont mean to preach code,
sizeof will only work on fixed-length arrays, which do not always behave like char*s. if you tried sizeof on a char*, you would get an error (system dependent length, such as 4). therefore, for good practice and readibility, you should declare a constant, len.

  
const int len = 10;
char str[len];
for(int index = 0; index < len; index++)...


when you work with c++, you need to keep in mind that arrays are zero-based, and when you dimension an array, the count is one-based.
therefore, str[10], will create 10 elements, 0 <= i <= count-1.
not to be confused with VB, 0 <= i <= count.
so as a rule, when you use for loops in c++, you always use i < count, and not i <= count.

Share this post


Link to post
Share on other sites
is there a good reason for using this code:

#include <string>
std::string

?

who''s programming in C nowadays ?
and the poor ones who do program i C.. why doesn''t he code in C++ instead ? C has no advantages that C++ doesnt have..

I have Visual C++ 6.0 installed and only 1 string.h file so I dont have any problems with multiple header files with the same name.. read that the std:: stuff is something about that..

Share this post


Link to post
Share on other sites
One thing that I don''t think was covered here:

Using sizeof() in an array loop isn''t a good habit to get into. Lets say you had an array of floats, then doing sizeof() on that is going to give you 4 times your actual array length (since it gives the number of bytes taken up by the array). Thus you''ll overrun into other memory, possibly causing a segmentation fault and/or other problems. It''s also possible that with char arrays, the system will allocate more than you ask for (some machines only allocate memory in blocks of 4 bytes) and sizeof may return that extra space.

If you''re hardcoding the size, use a #define or a const int and use that in the for loop. If the size can change, then your loop should check the maximum number of character, and it can stop when it hits a ''\0'' character.

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.