Help me String not printing :(

This is a discussion on Help me String not printing :( within the C++ Programming forums, part of the General Programming Boards category; String tel_s is not printing in "conv" function from line no: 76... I m sending tel_s from main to "void ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Help me String not printing :(

    String tel_s is not printing in "conv" function from line no: 76... I m sending tel_s from main to "void conv" which is assigned value there..

    Code:
    #include <iostream>
    
    using namespace std;
    
    void conv(string& age_s, string& tel_s, int& age, int& tel, int age_len, int tel_len);
    int intlen(int a);
    void inpt(string& fname, string& lname, string& add, int& age, int& tel);
    
    int main()
    {
        string fname, lname, add, age_s, tel_s;
        int age, tel, age_len, tel_len;
        float sall=0.00;
    
        inpt(fname,lname,add,age,tel);
        age_len=intlen(age);
        tel_len=intlen(tel);
    
        //cout << tel_len << endl << age_len << endl;
        conv(age_s,tel_s,age,tel,age_len,tel_len);
        cout << fname << endl;
        cout << lname << endl;
        cout << add << endl;
        //cout << tel_s << "hi" << endl;
        return 0;
    }
    
    void inpt(string& fname, string& lname, string& add, int& age, int& tel)
    {
       cout << "Enter First name: ";
        getline(cin,fname);
        cout << "Enter Last name: ";
        getline(cin, lname);
       cout << "Enter Address: ";
        getline(cin, add);
        cout << "Enter Telephone no: ";
        cin >> tel;
    
        cout << "Enter age: ";
        cin >> age;
    }
    
    int intlen(int a)
    {
        int b = 0;
        while(a > 0)
        {
            a = a/10;
            b++;
        }
        return b;
    }
    
    void conv(string& age_s, string& tel_s, int& age, int& tel, int age_len, int tel_len)
    {
    
       int temp=0, i=0,j=0;
       temp=tel;
       int div=1;
       char tel_c;
    
       for (j=0;j < (tel_len-1);j++){
       div=1;
       for (i=0; i<((tel_len)-(j+1)); i++)
       {
          div=div*10;
       }
    
        temp =(temp/div);
        cout << "temp: " <<temp << endl << "div: " << div<< endl;
        tel_c=temp+47;
        tel_s[j]=tel_c;
        temp=tel%div;
       }
    
    cout << "his" << tel_s;
    
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Your string has no size when it is passed to the function, and then you try and assign a value to a portion of string at [j], there is no string [j] from waht i can see, so nothing will be assigned.

    try adding a test:

    Code:
       if(tel_s.empty())
    	{
    		tel_s[j]=tel_c;
    	}
    	else
    		cout << "no string size!"
    Last edited by rogster001; 02-09-2012 at 02:10 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    sorry i had a typo in the example, it should be
    Code:
    if( !tel_s.empty() )
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Nit:
    Code:
    #include <string>
    Even if your code compiles without that line you should still have it in place.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    Still nt printing :(

    its still nt printing . Added string and ! .. plz
    output is also mentioned below.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void conv(string& age_s, string& tel_s, int& age, int& tel, int age_len, int tel_len);
    int intlen(int a);
    void inpt(string& fname, string& lname, string& add, int& age, int& tel);
    
    int main()
    {
        string fname, lname, add, age_s, tel_s;
        int age, tel, age_len, tel_len;
        float sall=0.00;
    
        inpt(fname,lname,add,age,tel);
        age_len=intlen(age);
        tel_len=intlen(tel);
    
        //cout << tel_len << endl << age_len << endl;
        conv(age_s,tel_s,age,tel,age_len,tel_len);
        cout << fname << endl;
        cout << lname << endl;
        cout << add << endl;
        //cout << tel_s << "hi" << endl;
        return 0;
    }
    
    void inpt(string& fname, string& lname, string& add, int& age, int& tel)
    {
       cout << "Enter First name: ";
        getline(cin,fname);
        cout << "Enter Last name: ";
        getline(cin, lname);
       cout << "Enter Address: ";
        getline(cin, add);
        cout << "Enter Telephone no: ";
        cin >> tel;
    
        cout << "Enter age: ";
        cin >> age;
    }
    
    int intlen(int a)
    {
        int b = 0;
        while(a > 0)
        {
            a = a/10;
            b++;
        }
        return b;
    }
    
    void conv(string& age_s, string& tel_s, int& age, int& tel, int age_len, int tel_len)
    {
    
       int temp=0, i=0,j=0;
       temp=tel;
       int div=1;
       char tel_c=49;
    
       for (j=0;j < (tel_len-1);j++){
       div=1;
       for (i=0; i<((tel_len)-(j+1)); i++)
       {
          div=div*10;
       }
    
        temp =(temp/div);
        cout << "temp: " <<temp << endl << "div: " << div<< "tel_c:" <<  tel_c<< endl;
        tel_c=temp+48;
        if( !tel_s.empty() )
     {
        tel_s[j]=tel_c;
     }
     else
     cout << "no string size";
        temp=tel%div;
       }
    
    cout << "tel:" << tel_s;
    
    }
    Output :
    Name:  2enq3ad.jpg
Views: 57
Size:  40.7 KB
    Last edited by fredsilvester93; 02-09-2012 at 12:20 PM.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    why dont you understand? You can see in your output that the new if() statement you added fails its condition, and outputs "no string size"

    In the start of your program you declare

    tel_s

    as a string object, then you dont do anything with it, the next time it is used is when you pass it (with NO size, ie meaningful content) to the conv() function.
    So what do you think that means when it gets to the if() test?

    The string is not assigned content in conv(); you cant just say:
    ' assign value of tel_c to tel_s at [j] '
    because tel_s at [j] does not exist

    This should serve to highlight certainly potential problems there are in the usability of your code as well as the example in question. Fair enough, it is not expected you write a huge amount of input validation lines in this type of exercise, but you should be keeping on top of the obvious ones.

    instead of "no string size" change for:
    "No String Size !! Nobody ever gave me any proper content :-( ...y en mis ojos no ha parado de llover :-< \n"

    put this in the "else cout" in your new if() statement if it helps hammer home the point any better in your output
    Last edited by rogster001; 02-09-2012 at 03:09 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing a string
    By weili in forum C Programming
    Replies: 1
    Last Post: 07-25-2011, 03:08 PM
  2. problem printing string
    By gkoenig in forum C Programming
    Replies: 4
    Last Post: 05-04-2008, 10:01 PM
  3. Printing String Arrays
    By kwikness in forum C Programming
    Replies: 6
    Last Post: 10-08-2007, 01:44 AM
  4. seg fault when printing string
    By ccoder01 in forum C Programming
    Replies: 6
    Last Post: 04-23-2004, 05:30 AM
  5. printing a string
    By ktntech in forum C Programming
    Replies: 1
    Last Post: 01-24-2002, 04:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21