I have this program and I want to fill the tables array with the values passed from the command line in integer form . However It string s is only being assigned argument 6 .. what is the problem ?
#include <iostream>
#include <cctype>
#include <locale>
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;
int main(int argc,char *argv[]){
int i;
int tables[100];
stringstream str;
string s;
int result;
char value;
if(argc <=1){
cout<<"NO ARGUMENTS PASSED"<<endl;
exit(0);
}
/*char value = *argv[1];
cout<<value<<endl;
str << value;
str >> s;
result = stoi(s,nullptr,10);
cout<<result<<endl;*/
for (i=1;i<argc;i++){
if(isdigit(*argv[i])){
value = *argv[i];
str<<value;
str>>s;
cout<<s<<endl;
tables[i-1] = stoi(s,nullptr,10);
}
}
}
isdigit function test if a char is a digit, so the command line
isdigit(*argv[i])
Return true is the firts character of the char* is a digit. What you want is to convert a char* to an integer, I suggest to take a look at the atoi function.
However, the string convertion for printing your result is not necessary.
The problem is that you are using stringstream in the wrong way.
By writing str >> s you are reaching eof in the stream.
To fix this, you can avoid to use stringstream and instead directly assign value to s.
If you want to use stringstream, you can reset it back to initial state after writing to s as follows:
str.str(std::string{});
str.clear();
and use it again
Related
I'm trying to use "scanf" to read a string line: "is it not working", but I don't know if it's even possible to implement it in this particular example.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "Just an example of, why ";
int number;
double doub;
string longText;
scanf("%d %lf %s", &number, &doub, &longText); //Read a line ex ("is it not working")
printf("%d\n%lf\n%s", number+i, doub+d,s+longText); //Print all the values, but not printing s+longText
}
Image showing the code
%s in printf/scanf family stands for char array, not std::string. If you want to get line, use std::getline. If you want to use std::string buffer for stdio functions, use std::string::data function, but I wouldn't suggest that as buffer-overflow is likely, especially for something like get-line.
I have a program that takes in a user input which can range from a 5 char command like "help" and to also support flag-type commands like "delete -p 'George'"
I don't have much experience with c++, other than doing a bunch of for loops, was wondering if there was a more effective way to parse the array of char.
Could someone point me to the right direction?
Aside from boost library as suggested in the comment, if you're parsing a relative small set of arguments, you can use simple std::cin for taking in arguments as the programme runs, something like:
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> args;
std::string arg;
while(std::cin >> arg) {
args.push_back(arg);
}
}
The above requires an EOF(not carriage return) to mark the end of commands.
For carriage return to mark the end of command, you'll need getline(), as demonstrated:
std::vector<std::string> get_args() {
using std::string;
using std::stringstream; // don't forget to include <sstream> header
string line;
getline(std::cin, line);
stringstream ss;
ss << line;
std::vector<string> cmds;
string cmd;
while (ss >> cmd) {
cmds.push_back(cmd);
}
return cmds;
}
Or if you'd like your main function to take arguments:
int main(int argc, char **argv) {
// The call to the excutable itself will be the 0th element of this vector
std::vector<std::string> args(argv, argv + argc);
}
Yes you can assign an char array to string like this:
char array[5] = "test";
string str (array);
cout << str;
output :
test
I have an input file which I'm reading in with the basic myFile >> variable since I know the format and the format will always be correct. The file I'm reading in is formatted as instruction <num> <num> and to make >> work, I'm reading everything in as a string. If I have 3 variables, one to take in each piece of the line, how can I then turn string <1> (for example) into int 1? I know the string's first and last characters are brackets which need to be removed, then I could cast to an int, but I'm new to C++ and would like some insight on the best method of doing this (finding and removing the <>, then casting to int)
use stringstream
#include <string>
#include <sstream>
#include <iostream>
int main() {
std::string str = "<1>";
int value;
std::stringstream ss(str);
char c;
ss >> c >> value >> c;
std::cout << value;
}
First to get the middle character out you can just do char myChar = inputString.at(1);. Then you can do int myInt = (int)myChar;
Even if you remove the <> characters, your still importing the file content into a string using >> so you still need to cast it to an int. If you have only 1 value, you can follow what Nicholas Callahan wrote in the previous answer, but if you have multiple characters you want to read as int, you dont have a choice but to cast.
You can also resort to sscanf.
#include <cstdio>
#include <iostream>
#include <string>
int main()
{
std::string str = "<1234>";
int value;
sscanf(str.c_str(), "<%d>", &value);
std::cout << value << std::endl;
}
Is it possible to check if a string variable is entirely numeric? I know you can iterate through the alphabets to check for a non-numeric character, but is there any other way?
The quickest way i can think of is to try to cast it with "strtol" or similar functions and see whether it can convert the entire string:
char* numberString = "100";
char* endptr;
long number = strtol(numberString, &endptr, 10);
if (*endptr) {
// Cast failed
} else {
// Cast succeeded
}
This topic is also discussed in this thread: How to determine if a string is a number with C++?
Hope this helps :)
#include <iostream>
#include <string>
#include <locale>
#include <algorithm>
bool is_numeric(std::string str, std::locale loc = std::locale())
{
return std::all_of(str.begin(), str.end(), std::isdigit);
}
int main()
{
std::string str;
std::cin >> str;
std::cout << std::boolalpha << is_numeric(str); // true
}
You can use the isdigit function in the ctype library:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main ()
{
char mystr[]="56203";
int the_number;
if (isdigit(mystr[0]))
{
the_number = atoi (mystr);
printf ("The following is an integer\n",the_number);
}
return 0;
}
This example checks the first character only. If you want to check the whole string then you can use a loop, or if its a fixed length and small just combine isdigit() with &&.
I want to get string input from the user. At the same time, I want to supply a default string so that if the user doesn't want to change it, they can just press enter. How can that be done in C++?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* args[])
{
const string defaultText = "Default string";
string str;
string tmp;
getline(cin, tmp);
if (!tmp.empty()) //user typed something different than Enter
str = tmp;
else //otherwise use default value
str = defaultText;
cout << str << endl;
}
You should be able to do it with the version of getline() defined in . You can use it like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
getline(cin,str);
// Use str
}
Just use two strings: Default string and User_supplied string. Get the input from the user (for the user_supplied string) and do an strlen on this string to check if it has a length greater than zero. If so use the User_supplied string, else use the default string