Simple question on quoting output

This is a discussion on Simple question on quoting output within the C++ Programming forums, part of the General Programming Boards category; This is a simple problem, but I just can't figure it out. In the following code, I need to have ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    Simple question on quoting output

    This is a simple problem, but I just can't figure it out. In the following code, I need to have the output of n in quotes. IE. The output of the void quote (int n) function should read:

    Integer: "5"

    and not Integer: 5

    Here is the code:

    #include <iostream.h>

    void iquote(int n)
    {
    cout << "Integer: " << n << endl;
    }

    void iquote (double n)
    {
    cout << "Double: " << n << endl;
    }

    void iquote (char n)
    {
    cout << "Character: " << n << endl;
    }

    void main ()
    {
    iquote(5);
    iquote (6.3);
    iquote ('C');
    }

    Thanks in advance!!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    void iquote(int n)
    {
    cout << "Integer: \"" << n << "\"" << endl;
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks!! Some times the simple things stump me.

    Maybe you can help me with this. How would I convert the void iquote (char n) to a string??

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Well, probably the easiest to use is the function sprintf(), which works exactly like printf, except it prints to a string. Like:

    char buff[100];
    char ch = 'A';

    sprintf( buff, "%c", ch );
    Code:
    #include <ip.hpp>

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74
    I thought I could simply do:

    void iquote (string n)
    {
    cout << "String: \"" << n << "\"" << endl;
    }

    but that's not working.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    void iquote (string n)
    {
    cout << "String: \"" << n.c_str << "\"" << endl;
    }


    or maybe...


    void iquote (string n)
    {
    cout << "String: \"" << n.c_str() << "\"" << endl;
    }
    Code:
    #include <ip.hpp>

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    74
    Tried that, but I'm getting error that string is an undeclared identifier. I'm including the string.h library, I thought that would do it, but no luck.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Ummm, no that's a C header. I think the C++ header is <cstr> or <cstring>...
    Code:
    #include <ip.hpp>

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    74
    I tried that one too (it's cstring), but no luck. ARGH!!

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    74
    Figured it out, it goes:

    void iquote (char string[])
    {
    cout << "The string is: " << string << endl;
    }

    iquote("Cindy");

    Thanks!!

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Funny, I thought you were using a C++ string...
    Code:
    #include <ip.hpp>

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    74
    Sorry! All this stuff confuses the heck out of me. I'm not sure I even know why what I'm using is not a c++ string ???!!!
    Thanks for your help!

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    No. You are using an array of chars, often called a "string" by C programmers. When the C++ Standard Template Library was developed, they came up with a "class string", which is of course, not simply an array of chars, but a wrapper for arrays of chars, having many usefull functions and interfaces. For instance, in C, the line:

    char s[] = "hello";
    char q[] = s;

    ...will not work at all. But with a C++ string, you can do:

    string s = "hello";
    string q = s;

    You should definately investigate the STL. It is a wonderful little library!
    Code:
    #include <ip.hpp>

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    74
    Wow, thanks for clearing that up. I am taking a c++ class now, learning about classes. Seems much easier. I took a c class a couple semesters ago. Wish my instructors could have cleared this up for me...
    Thanks again!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple fp_in question.
    By BluePudding in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2006, 08:18 AM
  2. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 12:59 PM
  3. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 07:59 AM
  4. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 12:33 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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