Thread: returning a string

  1. #1
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61

    returning a string

    I'm trying to make a function that checks a string arguement and depending on the string changes it and returns it. So they would enter say their name into char name[10] which would then be put into a function that checks if the name is Stinky if so it would change it to ryan.

    heres what I have tried.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <conio.h>
    
    char name[10];
    
    char check(char* name_check)
    {
    
    if(name_check == "Stinky")
    {
    name_check = "Ryan";
    }
    else
    {
    name_check = "Not Ryan";
    }
    
    return name_check;
    }
    
    int main()
    {
    cout<<"Enter your name: ";
    cin.getline(name, 10);
    name=check(name);
    return 0;
    }
    It comes up with two errors:
    ! Error E2034 .\test.cpp 19: Cannot convert 'char *' to 'char' in function * check(char *)
    ! Error E2277 .\test.cpp 26: Lvalue required in function main()

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    When using C-style strings you can't use the == or = operators for comparison and assignment. You must use strcmp and strcpy in the <string.h> (or <cstring> for Standard C++) header. Also, there's no need to return anything from check because 1) name is global, 2) You can't assign to it anyway and 3) Arrays are passed by simulated reference so any changes made will be mirrored in main. I think you want something more like this:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    void check(char* name_check)
    {
      if(strcmp(name_check, "Stinky") == 0)
      {
        strcpy(name_check, "Ryan");
      }
      else
      {
        strcpy(name_check, "Not Ryan");
      }
    }
    
    int main()
    {
      char name[10];
    
      cout<<"Enter your name: ";
      cin.getline(name, 10);
      check(name);
      cout<<name<<endl;
    
      return 0;
    }
    I'll address your errors now.

    >! Error E2034 .\test.cpp 19: Cannot convert 'char *' to 'char' in function * check(char *)
    Your check returns a char * (name_check), but the return type is char. This is a type mismatch. If you really want to return name_check then you need to change the return type to char *.

    >! Error E2277 .\test.cpp 26: Lvalue required in function main()
    Arrays aren't modifiable lvalues. In other words, you can't assign to an array as a whole.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    never mind, Prelude started before I did, or types faster than I do.

  4. #4
    Stinking it up. StinkyRyan's Avatar
    Join Date
    Jun 2004
    Posts
    61
    Thanks that is exactly what I wanted. I'm glad i had some what of the right idea though I'm just not used to working with strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM