Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
#include<fstream>
#include <iostream>
#include<string.h>
using namespace std;
int main(int argc, char* argv[])
{
string s = "all";
string t = "top";
for (int i=1; i<argc ; i++)
{
if( i == 2 && (argv[i] != s) && (argv[i] != t))
{
cout<<"INVALID MODE"<< endl;
}
if( i ==1 && (argv[i] != int))
{
cout<<"INVALID PHRASE LENGTH"<< endl;
}
}
}
I know this is horribly wrong, but is there away to do this ? im trying to say that when i =2, meaning argument 2 in a command line, is not all, and not top, to print out InValid mode
along with that, how would i test if an argument is not an integer and if it is not an integer to print out “INVALID PHRASE LENGTH” , but how would i test if the first argument is an integer or negative number
Not sure if you are aware, but when you run (sorry for running on windows)
program.exe arg1 arg2
then argv[0] is program.exe, argv[1] is arg1, argv[2] is arg2, so careful what you call the 1st and 2nd argument, meaning argv[1] is indeed the first string after the binary name, but only because of C++ indexing that starts from 0.
From what you are trying to achieve there is no need for the loop and iterating over the arguments.
#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char* argv[])
{
string s = "all";
string t = "top";
if (argc >= 3 && ! (argv[2] == s || argv[2] == t)) {
cout << "INVALID MODE" << endl;
}
}
There are plenty of questions answering parsing a string into int.
As said here though, picking up C++ beginner book is a better time investment than trying what compiles..
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Annotation:
In my code I have edited the string at an adneren position, this I have not had in my example.
Since here the actual error cause/problem lay I have adapted the example, see example below.
The Answer should be:
std::string.resize increases the memory size of the string to the maximum expected size.
replace inserts the entire size into str0.
A solution is to execute resize only if str1.lengh > max_len.
Original question:
I would like to insert a string (str1) into another (str0), at any position (n).
If str1 is shorter than the rest of string str0 (n -> str0.end()), the rest of str0 should remain.
For this I wanted to use the std::string::replace() function.
#include <string>
#include <stdint.h>
#include <iostream>
int main()
{
std::string str0 = "ABCDEFGHIJ";
std::string str1 = "000";
uint16_t n = 3;
str0.replace(n, str1.length(), str1);
std::cout << "TEST:" << str0 << std::endl;
return 0;
}
In my example, I expect to output "TEST: ABC000GHIJ" via cout.
However, "TEST: ABC000" is output.
Where is the problem that the string will be cutted?
Correct code example with the error cause:
#include <string>
#include <stdint.h>
#include <iostream>
#define max_len 20
int main()
{
std::string str0 = "ABDCDEF";
std::string str1 = "";
uint16_t n = 3;
std::getline(std::cin, str1);
if(!str1.empty() && str1[str1.size()-1] == '\n') {
str1.erase(str1.size()-1);
}
str1.resize(max_len);
str0.replace(n, str1.length(), str1);
std::cout << "TEST:" << str0 << std::endl;
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
My output file is supposed to show to answer to the function it calls on. The program runs fine, however it is not displaying the text in the "prime" function. the output file, when checked, only displays 1's. I believe this is due to the fact that its declared as a bool function, and set to return true. However, how would I get this code to return the solution in Prime to the output file?
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
bool prime(int);
int main()
{
int reader;
ifstream Infile;
Infile.open("numlist.txt");
ofstream outputFile;
outputFile.open("theoutput.txt");
while (Infile >> reader)
{
outputFile << prime(reader) <<endl;
}
Infile.close();
outputFile.close();
}
bool prime(int p)
{
if (p % 2 == 0)
cout << "\n" << p << "\n Is not a prime number";
else if (p % 2 != 0)
cout << "\n" << p << "\n is a prime number";
return true;
}
No errors, however the output file is only showing 1's.
This is happening because in your prime() function, all the output is going to cout and not into outputFile. The prime() function returns a bool which is what is sent to outputFile.
If you'd like to have output of the function go to outputFile, you can either pass outputFile as a parameter and use that instead of cout or make it global.
A few more comments on your code: you don't need the full else if (p % 2 != 0) in the else statement. You can just use else, because p % 2 is either 0 or it's not, there's no other option.
Also, strongly recommend using braces around if statements, even if they are just a single line.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Hello i'm from Indonesia. and i'm verry beginner on C++ programming. I have some problem when i learn about string on C++ . First i declared array of char and i want to initialize a value separately in different command. After i initialize the value my compiler say "Invalid Argument".
#include <iostream>
using namespace std;
int main() {
char Name[5];
Name = "Luke";
cout<<"Character 0 :"<<Name[0]<<endl;
cout<<"Character 1 :"<<Name[1]<<endl;
cout<<"Character 2 :"<<Name[2]<<endl;
cout<<"Character 3 :"<<Name[3]<<endl;
cout<<"Character 4 :"<<Name[4]<<endl;
return 0;
}
sorry if my english is bad :(
A character array(including a C string) can not have a new value assigned to it after it is declared.
The C++compiler interprets these assignment statements as attempts to change the address stored in the array name, not as attempts to change the contents of the array.
However you can use
char name[] = "Luke";
A char[] can't be assigned with a string with the = operator, except for on its initialization. That's why char Name[5]; Name = "Luke"; is invalid while char Name[5] = "Luke"; is.
Assigning strings to char[] can be done with strcpy() / memcpy()-like functions.
So you have two ways of action (assuming you want to work with char[]):
char Name[5] = "Luke";
char Name[5]; strcpy(Name, "Luke"); /* don't forget to #include <string.h>*/
Just for sake of education (since the other answers are on-point to answer the question), here's how I would have written your code to do nearly the same thing.
The changes demonstrate:
used a more appropriate container (a string instead of a char[])
checked for access overruns
moved "one unit of work" into its own subroutine
Code was compiled as C++17 with /usr/bin/clang++ -Weverything -Wno-c++98-compat --std=c++1z:
#include <cstddef>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
void PrintCharAtPos(string const& s, string::size_type pos);
int main() {
auto Name = string{"Luke"};
PrintCharAtPos(Name, 0);
PrintCharAtPos(Name, 1);
PrintCharAtPos(Name, 2);
PrintCharAtPos(Name, 3);
PrintCharAtPos(Name, 4);
return EXIT_SUCCESS;
}
void PrintCharAtPos(string const& s, string::size_type pos) {
if (pos < s.length())
cout << "Character " << pos << " : " << s[pos] << endl;
else
cout << "Character " << pos << " : (out of bounds)" << endl;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I can not figure out where the pointer is wrong in this code. However, I receive the error that the code does not have a pointer-to function.
#include <iostream>
using namespace std;
char uppercase (char ch) {
if ((ch >= 'a') && (ch <= 'z')) {
return ch - 'a' + 'A' ;
cout << "Your capital letter is " << ch << endl;
} else {
return ch;
cout << "Your original letter is: " << ch << endl;
}
}
int main(int& ch){
cout << "Please enter a lowercase letter between a to z: ";
cin >> ch;
char uppercase;
char outChar;
char inChar;
outChar = uppercase(inChar);
system("pause");
}
int main(char&) is not strictly-conforming. It may be provided by the implementation but I don't know of any platform doing this. On a hosted implementation, use int main() or int main(int argc, char** argv) instead.
Building up on the 1st note, declare ch in the function as a local variable and use char not int:
char ch;
Or remove it completely, as described in the 4th point.
You call uppercase on an uninitialized variable (inChar), resulting in undefined behavior because uppercase reads from it. Remove the ch variable and use cin on inChar instead.
You should probably exchange the return ch; with the cout-statement in the uppercase function. The cout-statement is dead code, meaning it will never be executed because the function returns beforehand.
you have local variable with same name as function name: uppercase
get rid of local variable
also fix main function to following signature.
int main(int argc, char *argv[])
{
....
}
ex:
#include <iostream>
using namespace std;
char uppercase (char ch) {
if ((ch >= 'a') && (ch <= 'z')) {
return ch - 'a' + 'A' ;
}
else {
return ch;
}
}
int main(int argc, char* argv[]){
cout << "Please enter a lowercase letter between a to z: ";
char inChar;
cin >> inChar;
char outChar = uppercase(inChar);
return 0;
}
I can see quite a few problems in your code:
Read Why System is evil. In short words, using system("pause") tends to be problematic. There are many other ways to make your code stop (e.g. another cin).
As one of the above comments stated, the general way of opening the main program is by using int main() or int main(int argc, char** argv).
Let's say that your code works... Then you're asking for an executable input "ch" which after that is changed again by asking the same input again during the program's run-time! I proppose that you use point 2. and what #cad said.
The true answer to your problem! You are using as an input to uppercase(char) a variable that has not been filled "InChar", but the character you are filling by user input is "ch".
Hope this helps.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I want to take argv[(2 in this example)], store it into vector and use it later in program. The problem is that no operand matches those operands std::string == int. So does that mean that the app sees '-r' as int? I'm a bit confused here.
int main(int argc, char* argv[])
{
std::vector<std::string> argList;
cout<<"argc: "<<argc<<endl;
for(int i=2; i<=argc; i++)
{
argList.push_back(argv[i]);
}
if(argList.at(2) == '-r') cout<<" Good job ";
}
There are several issues with your program:
You iterate i until i == argc, that will attempt to construct a string from argv[argc], a NULL pointer due to the requirement by C and C++ standards that argv[argc] be 0 (NULL), see this SO question. argv is an array with argc pointers to null-terminated character strings (terminated by ASCII NUL, 0), the array itself is terminated with a NULL pointer (not counted in argc). Now, in C++ you can construct a string from a pointer to a null-terminated character string, but passing a NULL pointer to a string constructor results in undefined behavior, see this SO question, also see the list of std::string constructors here, you are implicitly using constructor (4) in that list (from c-string).
You start pushing onto argList with i==2, which means argList[0] will contain argv[2], you then reference argList.at(2), which would correspond to argv[4], this is not likely what you meant.
String literals use double quotes
I've corrected these and created a working program, click here
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
int main(int argc, char* argv[])
{
vector<string> argList;
cout << "argc: " << argc << endl;
for(int i=0; i < argc; ++i)
{
argList.push_back(argv[i]);
}
cout << "Program name is " << argList[0] << endl;
if(argc > 1) {
if(argList.at(1) == "-r") {
cout << " Good job, you supplied -r\n";
} else {
cout << "Unrecognized option " << argList[1]
<< "\nUsage: " << argList[0] << " -r\n";
}
}
return 0;
}
The problem is your use of single quotes in '-r'. You want double quotes here:
if(argList.at(2) == "-r") cout<<" Good job ";
The reason is that in C++, single quotes are used for characters only. There is such a thing as a "multi-byte character constant", which is what '-r' ends up being. This is something completely different from a string constant, which is what you want.