Page 1 of 2 12 LastLast
Results 1 to 16 of 27

Thread: C++

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts

    Unhappy C++

    Hiya, I'm new to C++ and trying to get this code working. It should be simple but im getting an error message of:

    [C++ Error] SKS2Q2U.cpp(20): E2129 Character constant must be one or two characters long

    from this code:

    int Index;
    char space;
    AnsiString Line;
    space ='';

    Line = ReadStringPr("Enter a sentence:");
    for (Index = 1; Index <= Length(Line);Index= Index+1);
    if (Line[Index] == space)
    Line[Index] = '*';
    WriteStringPr ("the output is", Line);

    I don't understand where i am going wrong, in my code i have to replace all the spaces in a string with asterisks...

    any help would be appreciated,
    Thanks in advance

    Satz

  2. #2
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb
    Code:
    space ='';
    should be :
    Code:
    space = " ";
    and
    Code:
    Line[Index] = '*';
    should be
    Code:
    Line[Index] = "*";
    Also you have a semicolon after the first line of your for loop, so you need to remove that.. ok this should work:

    Code:
    int Index;
    char space;
    AnsiString Line;
    space = " ";
    
    Line = ReadStringPr("Enter a sentence:");
    
    for (Index = 1; Index <= Length(Line);Index= Index+1)
    {
    if (Line[Index] == space)
    Line[Index] = "*";
    }
    
    WriteStringPr ("the output is", Line);
    not 100% sure if that will work though..

    hth.
    Last edited by streetster; 24-05-2005 at 12:12 PM.

  3. #3
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    I always get my discrete mathematics mixed with programming sometimes. space = ''; is two of these things ' because it's a char. I'm a Java monkey so i'm applying it that way. I you use " those then it identifies a literal as a String but if you use ' then it identifies the literal as a char. Also, it's not usually a good idea to start a variable name with a capital (just because of conventions not because it's a syntactical error) so you can identify between variables and class names

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts
    Hiya i tried the amended coding u suggested and now i get this error message:

    [C++ Error] SKS2Q2U.cpp(19): E2034 Cannot convert 'char *' to 'char'

    To represent a space in C++ i thought it was '' becuse i want to assign space to ''
    "" double quotes makes it become a string doesnt it?

    But thanx for ur help .... i will keep trying but any more suggestions would be great!

  5. #5
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Quote Originally Posted by Satz
    "" double quotes makes it become a string doesnt it?
    That's what i thought *goes over the code again*

  6. #6
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Space is incorrect. Remember, you're interpreting a String not a char. You enter a string and you're looking for a space in that string so space = " "; will be correct

    To make it understandable, replace space = ''; with space = " "; (make sure you put the space inbetween the double quotation marks)

  7. #7
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Quote Originally Posted by Satz
    now i get this error message:

    [C++ Error] SKS2Q2U.cpp(19): E2034 Cannot convert 'char *' to 'char'
    thats complaining about converting a "pointer to a char" (char *) to an actual char, but I'm not 100% sure where or why.

    Old "C-style strings" were implemented as arrays of chars, and the identifier (variable) was really a pointer to the beginning of the array, so perhaps you're trying to covert a string to a char unsuccesfully. However I'm unfamiliar with the "AnsiString" type.

    Posisbly, to muddy the water a little more, you should be declairing space with single quotes Because...

    1. I have a vague recollection (My C++ is very rusty) that single quotes are used with chars, doubles with strings.
    2. In your original code you write:
    Code:
    space = '';
    (Two single quotes with nothing between them).
    Streetster suggested the quotes were the problem, but I think the mising space was, like Kezzer suggested. I think you should use:
    Code:
    space = ' ';
    (Single quote, space, single quote)Try this:
    Code:
    int Index;
    char space;
    AnsiString Line;
    space = ' ';
    
    Line = ReadStringPr("Enter a sentence:");
    
    for (Index = 1; Index <= Length(Line);Index= Index+1)
    {
    if (Line[Index] == space)
    Line[Index] = '*';
    }
    
    WriteStringPr ("the output is", Line);
    But to be honest, I don't think you need the space variable at all, chop it out and put the char in directly so:
    Code:
    int Index;
    char space;
    AnsiString Line;
    
    Line = ReadStringPr("Enter a sentence:");
    
    for (Index = 1; Index <= Length(Line);Index= Index+1)
    {
    if (Line[Index] == ' ')
    Line[Index] = '*';
    }
    
    WriteStringPr ("the output is", Line);
    If that doesn't work, any chance of some more information? For example what compiler are you using, and is this the whole of your file, or just the interesting bit?
    They told me I was gullible ... and I believed them.

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts
    WriteStringPr ("the output is", Line);
    [/CODE]But to be honest, I don't think you need the space variable at all, chop it out and put the char in directly so:
    Code:
    int Index;
    char space;
    AnsiString Line;
    
    Line = ReadStringPr("Enter a sentence:");
    
    for (Index = 1; Index <= Length(Line);Index= Index+1)
    {
    if (Line[Index] == ' ')
    Line[Index] = '*';
    }
    
    WriteStringPr ("the output is", Line);
    If that doesn't work, any chance of some more information? For example what compiler are you using, and is this the whole of your file, or just the interesting bit?[/QUOTE]


    I think its getting there ... firstly thanx to everyone that replied.
    Its working now!!!! I'm so releived i dont have the brain to do C++ its the little things that i miss out!

    But thank you. I'm using C++ builder5.
    My coding that i have to do is this as my first part so that all the spaces are replaced by an asterisk. Then i have to count how many times each letter occurs in the sentence where i would be using a case selector/swtich am i right? Im using functions in this too. So im off to tackle the next bit.
    any suggestions on the next bit as i have got:
    starts with another for loop as :

    for (Index=1; Index<= Length(Line); Index = Index +1)
    {
    if (Line[Index] != '*')

    case CountChar <--- This is a function that counts the occurance of a charatcter but i want to count all the characters so if i input "to be or not to be" i get:

    t occured 3 times
    o occured 4 times
    b occured twice
    e occured twice etc....
    here is mt design for this :

    1 Read in Line from the keyboard
    2 replace all space characters in line by '*'
    3 loop for all characters in Line
    4.1 if character is not an asterisk then
    4.2 write out character together with how many times it occurs
    4.3 replace all occurances of this character by '*'
    4.4 ifend
    4.5 loopend

    also im using another function called starChar that replaces a certain wanted character with a asterisk.

    thank you all for any suggestion keep them coming i will be much appreciated.

  9. #9
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Quote Originally Posted by Satz
    I think its getting there ... firstly thanx to everyone that replied. Its working now!!!!
    No Problem, glad to help. Just keep hanging around the forums, you'll learn tons (not just about c++).
    Quote Originally Posted by Satz
    here is mt design for this :

    1 Read in Line from the keyboard
    2 replace all space characters in line by '*'
    3 loop for all characters in Line
    4.1 if character is not an asterisk then
    4.2 write out character together with how many times it occurs
    4.3 replace all occurances of this character by '*'
    4.4 ifend
    4.5 loopend

    also im using another function called starChar that replaces a certain wanted character with an asterisk.
    Looks OK to me, give it a go and let us know how you get on.
    They told me I was gullible ... and I believed them.

  10. #10
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,899
    Thanks
    67
    Thanked
    180 times in 135 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    stytagm got it - '' is not a valid char literal in C++ because there's no character between the '.

  11. #11
    Registered User
    Join Date
    May 2005
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts
    Hiya thanx to all those that replied.
    Just another problem - im learning to write functions.... I have an excercise where i have to write a function for:

    CountChar
    String Str, Character Wanted
    Returns the number of occurances (zero or more) of the character Wanted in the string Str, for example, if Str is 'the cat sat on the mat' Wanted is 't' then 4 is returned.
    Integer CountChar(str,Wanted)

    can anyone help me write a code for this or any suggestion on how to get on wiv it?
    I know that i should use a for loop .
    i'm just a real total beginner as you can see!!
    thanks

  12. #12
    Senior Member
    Join Date
    Jan 2005
    Location
    Manchester
    Posts
    2,899
    Thanks
    67
    Thanked
    180 times in 135 posts
    • Butcher's system
      • Motherboard:
      • MSI Z97 Gaming 3
      • CPU:
      • i7-4790K
      • Memory:
      • 8 GB Corsair 1866 MHz
      • Storage:
      • 120GB SSD, 240GB SSD, 2TB HDD
      • Graphics card(s):
      • MSI GTX 970
      • PSU:
      • Antec 650W
      • Case:
      • Big Black Cube!
      • Operating System:
      • Windows 7
    You want a for loop which goes through each character in the string. You should have a counter variable which holds how many times you've seen the wanted character. Each time through the loop check if the currect char is the wanted char and increment the counter as appropriate.

    Not going to write any code here, if you have some code I'll point out errors though.

  13. #13
    Registered User
    Join Date
    May 2005
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts
    int Result;
    AnsiString Str;
    char Wanted;
    int Index;
    Str = ReadStringPr("Enter a sentence:");
    Wanted = ReadCharPr("Enter a character wanted:");
    for (Index = 0; Index >= Str(Length); Index=Index+1)
    {
    if (Str.[Index] == Wanted)
    Result = Result +1;
    return Result;
    }

    I got this so far but its returning a few errors,
    [C++ Error] funcq2b.cpp(18): E2314 Call of nonfunction in the for loop.
    [C++ Error] funcq2b.cpp(20): E2280 Member identifier expected in the if statement.

    Can some one help please?

  14. #14
    Registered User
    Join Date
    May 2005
    Location
    London
    Posts
    11
    Thanks
    0
    Thanked
    0 times in 0 posts
    oh and one more thing - because it is a function at the top i need to write
    void CountChar (AnsiString &Str, Char &Wanted).

    I don't understand the bit where i have to return the number of times a character is in the sentence as ive added the variable Result myself but in the excercise only two variables are given Str and Wanted and that is all that is needed really... so how do i add the characters and return the value?

  15. #15
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    You want somthing along these lines:

    int loop;
    int count = 0;
    char str[100];
    char = wanted[1];

    Get the string, place in str;
    Get the character, place in wanted;
    for(loop = 1; loop <= strlen(str); loop ++)
    {
    if(str[loop] == wanted)
    count++
    }
    return count;

    You don't want to return your result inside the "for" loop. strlen() will be the function used to get the length of the character array "str".
    To err is human. To really foul things up ... you need a computer.

  16. #16
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts
    Right it looks like you've got a little confused about your function, and what goes inside it and what goes outside.

    A function is like little mini section of your program, tucked away in a corner, and you call it from the main bit of your program. So the idea here is to write the function, that does the counting, and put it out of the way. Then when ever we want to count a sentence in the main bit of your program, we call the function, passing it the string and the char, it does the work and then passes you back the result, an integer I guess.

    So in your main program have the bits like:
    Code:
    Str = ReadStringPr("Enter a sentence:");
    Wanted = ReadCharPr("Enter a character wanted:");
    //Then call the function
    MyCountingFunction(str, wanted);
    That last line, calls your function, and passes it the string "str" and the char "wanted". If your function returns an int, that whole line will resolve to (turn into) the result. So you could print it to the screen with:
    Code:
    WriteStringPr (MyCountingFunction(str, wanted));
    BUT... For all this to work, you need to have written your function, and the compiler needs to know that it exists. To this end near the top of your file (at least before you want to use your function put in a "function declaration line" to tell the compiler "don't worry, I will write this function later" In this case you'd put in something like:
    Code:
    int MyCountingFunction(AnsiString &Str, Char &Wanted);
    then at the end of your code (out of the way, it can even be in a seperate file, but that's a little more complicated) you write the function propper, something like:
    Code:
    int MyCountingFunction(AnsiString &Str, Char &Wanted)
    {
    int result;
    // You counting code goes here.
    return answer;
    }
    The "return" statement passes the answer back to where it was called from. The "int" before the name of the function (where you had void) tells the compiler that you will be returning an int. Just put your counting code in the middle such that "answer" ends up as the answer! Yamangaman's code should give you a started (although I havnt' read it in detail, as I started writing this post before I noticed his!).

    Hope this makes some sort of sense, if not ask questions, It's Friday afternoon, and I don't want to be doing real work
    They told me I was gullible ... and I believed them.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •