[c++]Question about setw

Collapse
X
 
  • Time
  • Show
Clear All
new posts

  • Banfa
    replied
    Originally posted by whodgson
    Doesn`t `setw` set the width of the column to which the data is printed?; whereas \t sets the width of the column separating the 2 data columns.
    e.g.
    cout<<setw(3)<< i<<"\t"<<setw(5 )<<i*i<<"\n"; //might keep the data columns tidier.
    \t does not set the width to anything, it is a character and how it is handled normally depends on the terminal to which you send it.

    Commonly the terminal moves the output cursor to the next column multiple of 8.

    Leave a comment:


  • Banfa
    replied
    Originally posted by Simonius
    weaknessforcats am I doing something wrong in my code because I can't find anything about a stringstream only being capable of being used once.
    You can reuse a stringstream, however in doing so you need to reset it first before you try to use it call the method

    stringstream::c lear();

    on your instance of the class before trying to reuse it.

    Leave a comment:


  • whodgson
    replied
    Doesn`t `setw` set the width of the column to which the data is printed?; whereas \t sets the width of the column separating the 2 data columns.
    e.g.
    cout<<setw(3)<< i<<"\t"<<setw(5 )<<i*i<<"\n"; //might keep the data columns tidier.

    Leave a comment:


  • Simonius
    replied
    weaknessforcats am I doing something wrong in my code because I can't find anything about a stringstream only being capable of being used once.

    Leave a comment:


  • weaknessforcats
    replied
    Yes, that works but only for an int. If a double were entered, you will need to do something else to get the number of characters in the double.

    Leave a comment:


  • Banfa
    replied
    Originally posted by weaknessforcats
    Have you considered inserting the integer in a stringstream and recovering it as a string?? That way string::size() would be the number of characters in the integer.
    That seems like a rather conveluted way to me when a simple for loop dividing by the base you are interested in until you read 0 would achieve the same thing

    [code=cpp]
    int CountDigits(int number unsigned base)
    {
    int count;

    if (base == 0) // Prevent divide by 0
    {
    return 0;
    }

    for(count=1; number; count++)
    {
    number /= base;
    }

    return count;
    }
    [/code]

    Leave a comment:


  • Simonius
    replied
    Question, can a stringstream only be used once?
    Because while the following code seems ok to me, setw2 also equals 3 while it should be 5.
    I've run the program partially to before width2=.. to check where the error is and size seems to stay 100 while it should turn 100000.

    Code:
    // exercise 2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <sstream>
    #include <iomanip>
    
    using std::cout;		using std::stringstream;
    using std::string;		using std::setw;
    
    int main()
    {
    	int i=0, max, width1, width2;
    	max=100;
    	
    	{stringstream store;
    	store<<max;
    	string size;
    	store>>size;
    	width1=size.length();
    	i=max*max;
    	store<<i;
    	store>>size;
    	width2=(size.length());
    	}
    
    	for(i=0;i<=max;i++)
    		cout<<setw(width1)<<i<<setw(width2)<<i*i<<"\n";
    	system("PAUSE");
    	return 0;
    }

    Leave a comment:


  • Ganon11
    replied
    Yep. That's what I get for not actually having used the stringstream.

    Leave a comment:


  • weaknessforcats
    replied
    Originally posted by Simonius
    At first glance it looks to me like it should be
    SS << myInt;
    SS >> myStr;
    Yes it should. Just a typo.

    Leave a comment:


  • Simonius
    replied
    Hello, sorry for the late response but I've been quite busy.

    Which libraries & namespaces do I need for your example?

    And I could be mistaken due to not having tested it yet and not having worked with streams yet but didn't you switch the >> and <<?

    At first glance it looks to me like it should be
    SS << myInt;
    SS >> myStr;

    Leave a comment:


  • Ganon11
    replied
    Well, the reverse of int atoi(char*) is simply char* itoa(int), but these are both legacy C functions that should be avoided in C++. A stringstream will act like a stream (that's cin and cout) that can take your number and spit out a string, or vice versa, like this:

    [CODE=cpp]int myInt = 1234;
    string myStr;
    stringstream SS;
    SS >> myInt; // Read a value into the stream
    SS << myStr; // Extract a value from the stream
    // Now myStr is "1234"[/CODE]

    Leave a comment:


  • Simonius
    replied
    I know about atoi to convert from a string to a number, but I haven't found anything about the reverse.

    Leave a comment:


  • Simonius
    replied
    I would insert it into a string if I know how to convert from string to int and from int to string for the first time.
    A follow up question is to adapt it to another number.

    The book hasn't shown that much yet.

    Leave a comment:


  • weaknessforcats
    replied
    Have you considered inserting the integer in a stringstream and recovering it as a string?? That way string::size() would be the number of characters in the integer.

    Leave a comment:


  • Simonius
    replied
    The problem I'm having is determening the length of the integer.

    Either I use a left allign and use a width of the ammount of digits of the largest counters +1.

    Or I use a right allign of the ammount of digits of the largest result + 1.

    But I don't know how to get how many digits the number is.

    Leave a comment:

Working...