#include<fstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
for (int i=1; i<argc ; i++)
{
cout<< argv[i]<< endl;
if( argv[i] == 'all')
cout<<"hey"<< endl;
}
i am passing "1 all emptyfile" through the command files. but i want to compare when argue[i] is equal to all, i want it to print hey
i get this error "Comparison between pointer and integer ('char *' and 'int')" at the if( argv[i] == 'all') anyway i could do it ?
C-style strings can't be compared directly, and even worse, 'all' isn't a string (you probably meant "all", note the quotes).
The C-way to compare them is to use strcmp:
#include <cstring>
if (std::strcmp(argv[i], "all") == 0) { ... }
Since you are using C++, you can use std::string which will allow you to compare strings directly.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
string s = "all";
for (int i=1; i<argc ; i++)
{
cout<< argv[i]<< endl;
if( argv[i] == s)
cout<<"hey"<< endl;
}
}
Note: all is in double-quotes.
Output when you run the program:
>a.exe all
all
hey
Related
I have a char pointer , then i convert it into string format. So i would like to capitalize the first character of each word in this string, I wrote the code below :
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
void transform(char *s);
int main(int argc, char const *argv[])
{
char *str = new char[255];
strcpy(str, "jimmy catter");
string s;
s+=str; //convert char to string
std::cout<<s<<endl;
for (int i = 0; i < s.length(); ++i)
{
if (s[i] == ' ')
{
if (islower(s[i+1])==1)
{
s[i+1] = toupper(s[i+1]);
}
}
}
std::cout<<s<<endl;
return 0;
}
I would like to the output shoule be "Jimmy Catter" , but the result i got is still in the lower format. Could you please help look with this ?
So basically, there are two things here:
islower() should be treated as if it returns a bool, even if it says it returns an int. This is because for a bool, the actual value is false if 0 and true if any value other than 0. This means that if you wanted to still compare to a numerical value you would say islower(s[i]) != 0 since 1 is not the only valid internal value for true.
For the first character (J), there isn't a space before it. So, what we can do is check the current character is the first character OR if the previous character is space. Basically, instead of checking the current character and changing the next character, we check the previous character (or if it's the first character) and then change the current character.
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
void transform(char *s);
int main(int argc, char const *argv[])
{
char *str = new char[255];
strcpy(str, "jimmy catter");
string s;
s+=str; //convert char to string
std::cout<<s<<endl;
for (int i = 0; i < s.length(); ++i) {
if (i == 0 || s[i - 1] == ' ') {
if (islower(s[i])) {
s[i] = toupper(s[i]);
}
}
}
std::cout<<s<<endl;
return 0;
}
First thing to note here is that islower returns non zero(>0) number if argument is lowercase instead of 1.
Second you are trying to capitalize char by checking if char before it was whitespace. Nothing wrong with this approach but it won't work on first character so you can do it by adding an extra line before the loop. So with some tweaking it works fine now:
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
// void transform(char *s);
int main(int argc, char const *argv[])
{
string s = "jimmy catter ";
/*
char *str = new char[255];
strcpy(str, "jimmy catter");
string s;
s+=str; //convert char to string
*/
std::cout<<s<<endl;
s[0]=toupper(s[0]);
for (int i = 0; i < s.length()-1; ++i)
{
if (s[i] == ' ')
{
if (islower(s[i+1])>=1)
{
s[i+1] = toupper(s[i+1]);
}
}
}
std::cout<<s<<endl;
return 0;
}
You may use ASCII notations because they are quite clear to understand and execute in such programs.
I modified your code a little bit in the loop part and it gives correct output. Have a look at it.
for (int i = 0; i < s.length(); ++i)
{
if(!i&&97<=s[i]<=122) //handles border condition where first letter is small case
{
s[i] = s[i]-32;
}
if ((i!=s.length-1)&&s[i] == ' ') //prevents invalid output where last character is a space
{
if (97<=s[i+1]<=122) //checks if letter after space is small case
{
s[i+1] = s[i+1]-32;
}
}
}
std::cout<<s<<endl;
return 0;
}
Let's say I have an array
string test = {"test1, "test2"}
I have my function
void testing(string test){
for(int i = 0; i < 2; i++){
if(test[i] == "test1"){
cout << "success" << endl;
}
}
}
But when I compile this, I get an error...why is that?
Is there a different approach?
Your test variable should be declared as an array type
string test[] = {"test1", "test2"};
You also need to change the function signature from
void testing(string test)
to
void testing(string* test){
the code you wrote is not going to compile because of wrong declaration of string array.
replace
string test = {"test1, "test2"};
with
string test[]={"test1, "test2"};
The following code uses the array in place without function
#include <iostream>
#include <string>
using namespace std;
string test[]={"test1, "test2"};
for(auto& item:test)
{
cout<<item<<endl;
}
I think the best way to get this working with function is to use vector
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void testing(const vector<string>& strings)
{
for (auto& item : strings)
{
cout << item << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> strings = { "str1", "str2", "str3" };
testing(strings);
cin.get();
return 0;
}
So i'm experimenting with arrays when I come across a bit of a problem
code:
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char** argv) {
char items[] = {'bread', 'water', 'crisps', 'sweets', 'vegetables'};
for (int i = 0; i < strlen(items); i++) {
cout << items[i] << endl;
}
return 0;
}
What's happening is that when the code is ran, it's only outputting the last letter of each item, so 'd' 'r' 's' 's' 's'. I know i'm clearly doing something wrong here but I can't figure out what. I've been surfing on stackoverflow/google for a question like this but clearly what I have done is so obviously wrong, no one has asked!
Any help or a nudge in the right direction to particular documentation would be appreciated!
Thanks.
Need an array of character pointers.
Need to use double quotes
Read a book on C++
i.e. code should be
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char** argv) {
char *items[] = {"bread", "water", "crisps", "sweets", "vegetables"};
for (int i = 0; i < (sizeof(items) / sizeof(*items)); i++) {
cout << items[i] << endl;
}
return 0;
}
Any of these items in the initializer list
{'bread', 'water', 'crisps', 'sweets', 'vegetables'};
is a multicharacter literal. According to the C++ Standard
A multicharacter literal, or an ordinary character literal containing
a single c-char not representable in the execution character set, is
conditionally-supported, has type int, and has an
implementation-defined value.
You need to use string literals. I think that what you want is the following
#include <iostream>
int main(int argc, char** argv)
{
const char *items[] = { "bread", "water", "crisps", "sweets", "vegetables" };
for ( const char *s : items ) std::cout << s << std::endl;
return 0;
}
I need to save all arguments to a vector or something like this. I'm not a programmer, so I don't know how to do it, but here's what I've got so far. I just want to call a function system to pass all arguments after.
#include "stdafx.h"
#include "iostream"
#include "vector"
#include <string>
using namespace std;
int main ( int argc, char *argv[] )
{
for (int i=1; i<argc; i++)
{
if(strcmp(argv[i], "/all /renew") == 0)
{
system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");
}
else
system("c:\\windows\\system32\\ipconfig.exe"+**All Argv**);
}
return 0;
}
i need to save all arguments to a vector or something
You can use the range constructor of the vector and pass appropriate iterators:
std::vector<std::string> arguments(argv + 1, argv + argc);
Not 100% sure if that's what you were asking. If not, clarify.
To build string with all argument concatenated and then run a command based on those arguments, you can use something like:
#include <string>
using namespace std;
string concatenate ( int argc, char* argv[] )
{
if (argc < 1) {
return "";
}
string result(argv[0]);
for (int i=1; i < argc; ++i) {
result += " ";
result += argv[i];
}
return result;
}
int main ( int argc, char* argv[] )
{
const string arguments = concatenate(argc-1, argv+1);
if (arguments == "/all /renew") {
const string program = "c:\\windows\\system32\\ipconfig.exe";
const string command = program + " " + arguments;
system(command.c_str());
} else {
system("\"\"c:\\program files\\internet explorer\\iexplore.exe\" \"www.stackoverflow.com\"\"");
}
}
I've been trying to run a program that will invert the order of a string and to run it, I have to type a second argument in prompt.
int main(int argc, char* argv[])
{
string text = argv[2];
for (int num=text.size(); num>0; num--)
{
cout << text.at(num);
}
return 0;
}
e.g. ./program lorem result: merol
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string text = argv[1];
for (int num=text.size() - 1; num >= 0; num--)
{
cout << text.at(num);
}
return 0;
}
You missed the includes and used string::at wrong. There are size() chars in the string but you start counting at 0. Then the loop has to run until num >= 0 and not num > 0. You also used the wrong index into argv.
This would still be an abomination of C++. A clearer way would be:
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char* argv[])
{
std::string text = argv[1];
for(std::string::reverse_iterator it = text.rbegin(); it != text.rend(); ++it) {
std::cout << *it;
}
std::cout << std::endl;
//or if you want further usage of the reversed string
std::reverse(text.begin(), text.end());
std::cout << text;
return 0;
}
I think you're getting an exception because num is out of bounds. size() is returning a value one larger than the biggest valid index into the string, so at() is throwing an exception.