Thread: sad overly newbish question on output

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    sad overly newbish question on output

    In C++ how may I ask can I output a string as with fgets in C. What is its equivalent.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I may be wrong, as I started with C++, but I believe printf and cout (iostream.h, but only in console applications) will suit what you're looking for.

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    I'm not sure if a string (if you're talking about the STL class) will work with printf();. Use cout<< anyway - it's loads easier.
    Away.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    cin.getline(buffer_to_hold_input, num_of_chars, char_deliminator)

    If you are using the std::string, the function differs a bit.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    17

    Response

    Use cout<<" "; as has been said before.

    Example of <<endl;

    cout<<"this is a string."<<endl; //this creates a new line.

    Escape sequences are also allowed as shown.

    cout<<"\tthis is a string.\n";

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Lack of CLarity

    Im sorry, I should have said. (string variable)
    the only way I can print out the charectors in a string at the moment is.......

    for(x=0;x<sizeofstring;x++){
    cout << string[x];
    }


    this becomes, tedious, and Im sure that there is a command that will allow me to print the string en masse. I appologize for my lack of clarity, I do know the basics of C, and, what little I derived from a high school class, C++.
    Last edited by parrellel; 07-06-2003 at 10:09 AM.

  7. #7
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    If you're using the string class from <string> then cout will work. If you're using a nul terminated array of char, then cout will still work. If you're using some user defined class that doesn't overload operator<< then cout won't work. Can you show us the string variable declaration?

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    17
    Why are you iterating through the string? You're not changing variables. As has been said before you don't need to do it that way, just do this cout<<string; and the whole string will be outputed to the console window.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    output is not the string by writing

    cout << array;

    output is a memory address. This is what you instruct me to do? Accessing the memory address, expectedly, offers me the first peice'o'array not the area en tota.

    Or am I being horribly stupid.

  10. #10
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    Can you post your code already? You've given us useless little snippets so far, we need more to figure out what your exact problem is.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Yes, we need code. At this point I'm assuming your using std::string from when you said string variable. But it could be a char array. Post the code, and we'll be able to help you more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output Question.
    By kinghajj in forum C Programming
    Replies: 4
    Last Post: 07-15-2003, 04:40 PM
  2. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  3. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 12:11 PM
  4. Simple question on quoting output
    By LouB in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2002, 02:57 PM
  5. Output question
    By Drew in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2001, 05:45 PM