Thread: functions won't work

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    functions won't work

    before i explain my problem i'll give you what i've got so far
    Code:
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
     int question ( string usr_name );
    int main()
    {
    string usr_name;
    cout<<"hello enter a name: ";
    cin>> usr_name;
    cin.ignore();
    cout<<"ok so your name is "<< question ( usr_name );
    Sleep (20000);
    }
    
    int question ( string usr_name )
    {
      return usr_name;
    }
    now as you can probably gather i'm trying to make a function out of the code above and i think i've got it but when i compile i get an error on the
    Code:
     {
      return usr_name;
    }
    part of it, i've scanned tutorials but i can't find my problem or how to solve it, i realize this is probably some basic problem involving a misplaced semicolon or somethiing but i'd appreciate it if someone can help me out with the problem anyhow. thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    int question ( string usr_name )
    Well you pass it a string, and try and return an int.
    Make the types compatible.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    eg, make question() return a string.

    BTW, you should include <string> if you're going to be using strings.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Actually, <iostream> includes <string> for many of it's functions, so it's not really necessary.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Actually, <iostream> includes <string> for many of it's functions, so it's not really necessary.
    You mean your iostream includes string.
    Other people's compilers may not have the same internal dependencies.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Actually, <iostream> includes <string> for many of it's functions, so it's not really necessary.

    Many people have complained that cout << myString doesn't work when myString is a string and they end up using cout << myString.c_str(). The source of there error is not including <string>. That is because at least one popular compiler library includes only parts of <string> inside <iostream>.

    Never rely on one header including another header, always explicitly include any header that you need in your file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Trying to do basic math functions. Why doesn't this work?
    By crazychile in forum C Programming
    Replies: 5
    Last Post: 10-25-2008, 05:14 PM
  4. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  5. Replies: 6
    Last Post: 05-06-2003, 03:08 PM