I want to insert a new line feed within an array such that if the string length of my array increases beyond say 14 here the further contents of the array when displayed are displayed in a new line in the output console. Foe e.g Here in below program I want to Display "mein hoon don" in first line and after these 14 characters. I want to DISPLAY next content "Don Don Don" in a new line in output console. I read that using \0xa(hexa) and \10 in decimal are newline feed . But When i tried to use them in my code , I was not able to produce desired output.
# include <iostream.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
char abc[40];
strcpy(abc,"mein hoon don.");
abc[15]='\10';
abc[16]='\0';
strcat(abc,"Don Don Don");
cout << "value of abc is " << abc;
getchar();
}
Change:
abc[15] = '\10';
abc[16] = '\0';
to:
abc[14] = '\n';
abc[15] = '\0';
The escape sequence \10 does not do what you think it does. It is probably easiest to use \n instead.
Note: you could also use strcat to insert the newline and prevent any index calculation problems:
strcpy(abc,"mein hoon don.");
/* abc[15]='\10'; */
/* abc[16]='\0'; */
strcat(abc, "\n");
strcat(abc,"Don Don Don");
You are really better off using std::string instead of char arrays:
#include <iostream>
#include <string>
int main() {
std::string s;
s = "mein hoon don.";
s += "\n";
s += "Don Don Don";
std::cout << value of abc is " << s << std::endl;
return 0;
}
Well, the escape secuence depends on the operating system you are working: there are two caracters \r (carrier return) and \n (line feed), in windows, you need both, in linux, you need only \r and in Mac, you need \n.
but, as long as you are using cpp, you shouldnt use any of them, you should use endln instead:
//# include <stdio.h>
//# include <stdlib.h>
//# include <string.h>
#include <ostream>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
//char abc[40];
std::ostringstream auxStr;
auxStr << "mein hoon don." << std::endl << "Don Don Don" << std::endl;
//if you need a string, then:
//std::string abc = auxStr.str();
std::cout << "value of abc is " << auxStr.str();
getchar();
}
This code introduces a line break condition at position x.
Just set Line_Break=14; and you are done.
# include <iostream.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
int LinePos=0;
int Line_Break_Pos=0;
int Line_Break=10;
char abc[40]={""};
strcpy(abc,"mein hoon don. ");
strcat(abc,"Don Don Don");
cout << "\n" ;
cout << "Without linebreak : \n";
cout << abc ;
cout << "\n\n" ;
cout << "With linebreak : \n" ;
while (LinePos < strlen(abc) )
{
cout <<abc[LinePos];
if (Line_Break_Pos > Line_Break && abc[LinePos ]==' ')
{
cout << "\n" ;
Line_Break_Pos=0;
}
LinePos++;
Line_Break_Pos++;
}
cout << "\n\n" ;
return 0;
}
Related
I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"D:\\testlas\\BigOne.pcd",(char*)"D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?D:\\testlas\\BigOne.pcd
m.name: ?D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?
std::string test = "abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.
I'm working on a small program that counts up to a number given by the user. The number they enter is stored in the variable limit. I want the number in that variable to be displayed in the title kind of like this: "Counting up to 3000" or "Limit set to 3000" or something like that. I've tried using SetConsoleTitle(limit); and other things but they just don't work. With the code that I have posted bellow, I get the following error:
argument of type "int" is incompatible with parameter of type "LPCWSTR"
I'm currently using Visual Studio 2015 if that's important in any way.
#include <iostream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
int main()
{
begin:
int limit;
cout << "Enter a number you would like to count up to and press any key to start" << endl;
cin >> limit;
SetConsoleTitle(limit); // This is my problem
int x = 0;
while (x >= 0)
{
cout << x << endl;
x++;
if (x == limit)
{
cout << "Reached limit of " << limit << endl;
system("pause");
system("cls");
goto begin;
}
}
system("pause");
return 0;
}
The SetConsoleTitle() function expects a string as its argument, but you're giving it an integer. One possible solution would be to use std::to_wstring() to convert an integer to a wide-character string. C++ string that you get as a result has a different format from the null-terminated wide-character string that SetConsoleTitle() expects, so we need to make the necessary conversion using the c_str() method. So, instead of
SetConsoleTitle(limit);
you should have
SetConsoleTitle(to_wstring(limit).c_str());
Don't forget to #include <string> for to_wstring() to work.
If you want a title that includes more than just a number, you'll need to use a string stream (a wide character string stream in this case):
wstringstream titleStream;
titleStream << "Counting to " << limit << " goes here";
SetConsoleTitle(titleStream.str().c_str());
For string streams to work, #include <sstream>. Here's the full code:
#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
#include <stdlib.h>
using namespace std;
int main()
{
begin:
int limit;
cout << "Enter a number you would like to count up to and press any key to start" << endl;
cin >> limit;
wstringstream titleStream;
titleStream << "Counting to " << limit << " goes here";
SetConsoleTitle(titleStream.str().c_str());
int x = 0;
while (x >= 0)
{
cout << x << endl;
x++;
if (x == limit)
{
cout << "Reached limit of " << limit << endl;
system("pause");
system("cls");
goto begin;
}
}
system("pause");
return 0;
}
My program worked like it was supposed to until I added the toupper part into my program. I've tried looking at my error code but it's not really helping. The errors are:
no matching function to call
2 arguments expected, one provided
So I know the error is in those two statements in my while loop. What did I do wrong?
I want to make a name like
john brown
go to
John Brown
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main(){
string firstname[5];
string lastname[5];
ifstream fin( "data_names.txt" );
if (!fin) {
cout << "There is no file" << endl;
}
int i = 0;
while( i < 5 && (fin >> firstname[i]) && (fin >> lastname[i]) ) {
firstname[0] = toupper(firstname[0]);
lastname[0] = toupper(lastname[0]);
i++;
}
cout << firstname[0] << " " << lastname [0] << endl;
cout << firstname[1] << " " << lastname [1] << endl;
cout << firstname[2] << " " << lastname [2] << endl;
cout << firstname[3] << " " << lastname [3] << endl;
cout << firstname[4] << " " << lastname [4] << endl;
return 0;
}
std::toupper works on individual characters, but you are trying to apply it to strings. Besides adding #include <cctype>, you need to modify your while loop's body:
firstname[i][0] = toupper(firstname[i][0]);
lastname[i][0] = toupper(lastname[i][0]);
i++;
Then it should work as expected. Live demo here
As M.M helpfully pointed out in the comments, you should also check that your strings aren't empty before accessing their first characters, i.e. something like
if (!firstname[i].empty()) firstname[i][0] = toupper(...);
is strongly recommended.
Mind you, you will probably need more sophisticated logic if you get names like McDonald :)
You need ctype.h to get the proper definition for toupper(). It is usually implemented not as a function, but an array mapping.
#include <ctype.h>
The program has several flaws: using a string array instead of a string, not iterating through the string correctly, not declaring but using the C definition of toupper(), not exiting when the file does not exist.
Use this instead:
#include <ctype.h>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
ifstream fin ("data_names.txt");
if (!fin)
{
cerr << "File missing" << endl;
return 1;
}
// not sure if you were trying to process 5 lines or five words per line
// but this will process the entire file
while (!fin.eof())
{
string s;
fin >> s;
for (i = 0; i < s.length(); ++i)
s [i] = toupper (s [i]);
cout << s << endl;
}
return 0;
}
I want to know how can I make the string I converted from DWORD to onstringstream and then to AnsiString.
But that doesn't really matter, the conversion could be from int to string, I just want to know how I can make every string converted to ALWAYS show 6 digits, like if my number is 57, in the string it will be 000057.
Thanks!
Use io manipulators setfill and setw:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
std::ostringstream s;
s << std::setfill('0') << std::setw(6) << 154;
std::cout << s.str() << "\n";
return 0;
}
So, the question about formatted output?
you can use iostream::width and `iostream::fill':
// field width
#include <iostream>
using namespace std;
int main () {
cout << 100 << endl;
cout.width(6);
cout.fill('0');
cout << 100 << endl;
return 0;
}
Hello
I know it was asked many times but I hadn't found answer to my specific question.
I want to convert only string that contains only decimal numbers:
For example 256 is OK but 256a is not.
Could it be done without checking the string?
Thanks
The simplest way that makes error checking optional that I can think of is this:
char *endptr;
int x = strtol(str, &endptr, 0);
int error = (*endptr != '\0');
In C++ way, use stringstream:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream sstr;
int a = -1;
sstr << 256 << 'a';
sstr >> a;
if (sstr.failbit)
{
cout << "Either no character was extracted, or the character can't represent a proper value." << endl;
}
if (sstr.badbit)
{
cout << "Error on stream.\n";
}
cout << "Extracted number " << a << endl;
return 0;
}
An other way using c++ style : We check the number of digits to know if the string was valid or not :
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
int main(int argc,char* argv[]) {
std::string a("256");
std::istringstream buffer(a);
int number;
buffer >> number; // OK conversion is done !
// Let's now check if the string was valid !
// Quick way to compute number of digits
size_t num_of_digits = (size_t)floor( log10( abs( number ) ) ) + 1;
if (num_of_digits!=a.length()) {
std::cout << "Not a valid string !" << std::endl;
}
else {
std::cout << "Valid conversion to " << number << std::endl;
}
}