Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Help with C++

Name: Anonymous 2006-04-23 7:29

Hey people. I'm a physics student trying to teach myself C++ since my college doesn't offer it. As I go along the book, I try to test and implement features to try and figure out how they work. So far, so good. But now I'm trying to understand how strings of characters work, and I got the following problem.

I wrote the follow program:
#include <iostream>
using std::cout;

int catprint(char string[12])
{
int n = 0;
  while()
  {
    if(string[n]==0) break;
    cout << string[n];
    n++;
  }
 
  return n;
}

int main()
{
  char hello[12] = {'H','e','l','l','o',' ','W','o','r','l','d',0};
  int finalcount;
 
  finalcount = catprint(string[]);
  cout << "\Number of characters in string: " << finalcount << ".\n";
  cout << "Next char: " << hello[finalcount+1];
 
  return 0;
}

Name: Anonymous 2006-04-23 7:31

Sorry, line in main should be
finalcount = catprint(hello[]);

Name: Anonymous 2006-04-23 7:56

Pointers aren't just an efficiency thing; they're actually required for some problems. Replace int catprint(char string[12])

Name: Anonymous 2006-04-23 8:32

Right. I cleaned up the code a bit. I had forgotten a 0 in the while() and a missing n after a \ was fudging things up. I applied your changes and still get this:
print.cpp: In function `int main()':
print.cpp:11: error: cannot convert 'char (*)[12]' to 'char*' for argument '1' to 'int catprint(char*)'
which has me puzzled as according to my book, the expressions you gave me seem right. What's going wrong?

Name: Anonymous 2006-04-23 11:52

>>4
you're trying to convert a pointer to an array of char "char (*)[12]" to a pointer to a char "char*", which doesn't work.

finalcount = catprint(&hello[0]);

Name: Anonymous 2006-04-23 12:50

Rock on. Changed the 0 in while() to 1 and all went smoothly.

Thanks 4chan!

Name: Anonymous 2009-09-05 17:09

[/rem]

Name: Anonymous 2010-06-07 6:37

Hi, I can spam /prog/ too, you faggot.

Also, smoke weed everyday.

Name: Anonymous 2011-01-18 13:03

>>7

REM hi
10 PRINT "hi"
20 GOTO 10

Name: Anonymous 2011-01-18 13:45

Name: Anonymous 2011-01-31 19:45

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List