is setw() and "\t" the same thing? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Is setw() and "\t" the same thing tho?
And are they similar to "space" too?.
Can I use setw() in the place of "\t" or would it result in a completely different output?

They have almost nothing in common.
std::setw(int n) set the width of the next element that goes into the stream. So if you have things like:
std::cout << "Hi," << std::setw(12) << "there!";
This would print:
Hi, there!
^^^^^^ <- 6 empty spaces were made here to fill the width
If you set the width to be longer than the actually object streamed in to it, it will automatically fill them with spaces.
On the other hand, '\t' is a predefined escape sequence. And it will behave similar to when you type a tab in many text editors. Also note that it is actually a character, you could put that in any strings:
std::cout << "\tHi,\tthere!";
This would print:
Hi, there!
^^^^ ^ <-- both of them are tabs
Note those tabs were made different sizes, you should be able to observe similar behaviors when using tabs in text documents. It will try to fill the current 4 block text with spaces if it was not filled yet.

No they are not same at all.
"\t" : allocates 4 spaces;
setw() : setWidth() is a function defined in iomanip header file.
It takes a integer as parameter and allocates the width of value of the integer.
Take for an example : setw(7) It will allocate 7 spaces to you
cout<<setw(7)<<"Hi"<<"**";
Output will be : Hi + 5 Spaces + **
5 Spaces because in total you requested 7 spaces and 2 are occupied by 2 character of word "Hi".
This function is highly used where you want to display something in a very proper format.

Related

How to process a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The first line of input will be 2 integers, h and w, which is the height(h) and width(w) of the rectangular area of sky you will be counting stars from. You may assume their values are below 1000.
The following h lines will have w characters per line, either x (Clear Sky) or * (star).
Sample Input
5 5
x***x
xxx*x
x*xxx
xxx*x
*xxxx
Sample output
7
How can I process the string?
Declare a variable to count the stars with an appropriate data type.
Iterate (loop) over the string to check each character for equality with '*'. If this is the case then increment your star counter.
Width and height are not required. If you want to constrain a larger field to the width and height provided, you can use a column (character# in a line) and a row (line#) counter and keep track where in the file you are.
After each character in your string increment (add 1) your column counter. After each line ('\n'-character denotes a new line) increment your row counter and reset the column counter. if your current column or row exceed the provided width or height, then ignore any '*'-characters until you are within bounds again or the string ends.
I leave the actual code for you as an excercise.
Helpful links:
Strings
Operators
Conditionals
If you have trouble with the input part, you should consider looking elsewhere than stackoverflow.

How can i get all the text until a space don't come in Qstring? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hello i want to get the ultil the space in line occur for example
in the given picture am able to get the whole string using getter method to my parent window but i dont want get the date time part
am currently getting
https://www.youtube.com/watch?v=VXw6OdVmMpw Sun Nov 1 20:29:30 2015
while i want get https://www.youtube.com/watch?v=VXw6OdVmMpw
can anyone help me i used Qstring left right but i cant get it working properly.
//QString Input holds your data...
QString Output = Input.section(' ', 0, 0);
section sees the string as fields seperated by a seperator character (' ' in this case) and returns the section from the given start section (0) upto and including the end section (0).
Assuming the white space in your inputstring is really spaces, if it is not, change ' ' to the correct seperator character.

C++ -- Storing from .txt file to array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
2 Questions:
The first question is, I need to input the name of the text file when the program runs so let's say it's called "Banana.txt" , I type it in and I'm missing the last word of the file. This is basically all I used to output from the txt file.
while(File.good()){
cout <<word << " "; //put spaces in between the words
File >> word; }
Not sure how to get the last word.
The second question is how do I store the information from the .txt file into an array that I can use later?
Does it have to be multidimensional? The maximum words in the file are 100..
Eventually I'm going to need to ignore any words < 4 characters.
I actually can't recall if I should use char or string. But iirc char is each individual character whereas string is a collection of characters? Arg. Scratch that, Looking it up in a bit..
Not allowed to use hash_, vectors, maps, stack, or lists so I'm not sure how to go about this problem
Thanks in advance for the help. I tried looking through other threads but I'm not sure if those are the ones I'm looking for... Sorry for the questions ..
You're outputting before you've input.
Try swapping your two lines in the while loop, something like:
while(File.good())
{
File >> word;
cout << word << " "; //put spaces in between the words
}
to put the data into a container, assuming word is of type string, try:
vector<string> v;
and at the bottom of your loop:
v.push_back(word);
For the first one, you need to first read into the word then output it.
The "intuitive" loop of while(File.good()) though is flawed because if you are at end of file but haven't read it yet, the stream will appear good until you try reading the next word.
As a result the read will fail. You could do
while( File.good() )
{
if( File >> word )
{
// process this word
}
}
but simpler is
while( File >> word )
{
// process the word
}
Another thing you can use is istream_iterator and copy, and then you can actually copy them into your sequence. However you are not allowed to use the standard C++ library.

Place Commas Between Numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm having trouble getting this to work in AS3, I want to place a comma between numbers only when their is a whitespace.
For example, if the string is "1.23 5.34" I want it to become "1.23, 5.34". The trouble is the white space varies and the number may or may not contain a decimal. So, I'd want "1 1.4" to become "1, 1.4" or "2.3 4.5" to become "2.3, 4.5". This also includes negative numbers, so "1.4 -15.3" should become "1.4, -15.3". If there is anything but a number on either side of the white space, I'd want to skip that space and not effect it. So "Car 35.2" would be skipped and so would (13.5 ).
I've tried several Regexs found around the net and did my best with the limited regex knowledge I have, any help would be greatly appreciated.
Thanks.
UPDADE
(?<=\d)(\s)(?=-?\d) (thanks for your comment Tim)
Try the folowing pattern:
"(?<=\d)(\s)(?=[\d-])" (edited to include negative ones)
replace for ",$1"
youre essentially replacing " " with ", "
var value:String = "1 2 -3 4 -5";
var csvValue:String = value.split(" ").join(", "); // will print out "1, 2, -3, 4, -5"

C++ \n in a string (Not working) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm learning C++. Here's my problem.
http://prntscr.com/2m5flm
I created a function who can read Prop files like them (you can set the file beginning and ending tags with a function, searching a specified tag with a function who will return a string (containing the results).
Here's the main.cpp
#include <iostream>
#include "m_PFile_r.h"
using namespace std;
int main(int argc, const char * argv[])
{
m_PFile_r k;
k.open("prop.arg");
k.imNotaFag(true);
k.setOpenArg("$FILE_BEGIN$");
k.setCloseArg("$FILE_END$");
string lS;
lS=k.getArg("launchSentence");
cout << lS << endl;
string menu;
menu=k.getArg("progMenu");
cout << menu;
return 0;
}
MY QUESTION IS : Why doesn't it print the \n as a line return ?
Thanks :)
The file has new line characters in it, they are defining the end of the lines. When you enter the newline character in the file, it is being stored in that file not as a newline character, but the two individual characters "\" and "n". So when you then read in the file, those characters are read in just like the others.
You are over complicating this problem. If you would like to print out various phrases to the user, just include those phrases as string variables in your program.
String launchSentence = "This is the launch sentence.";
String progMenu = "Hit 1 For Add Hit 2 for Subtract";
These could then be printed with the normal COUT << progMenu method.
If your purpose with the text file is to keep all of the possible text strings isolated in one easy location, why not create a TextCommandPrompts.h, fill it with the String (character in C++) variables and include that in your main?
Edit - Because I can't comment yet and I want to respond to one - I thought that whatever text editor that was letting him write line by line would be messing this up. As in, its already doing the "\n" magic, and when he writes in the characters '\' and 'n' something mundane happened, and they stayed as regular characters.