Trying to convert a string to double under conditions [closed] - c++

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 6 years ago.
Improve this question
I have a function which uses an std string parameter to test if there is an alpha character. It would be best to test for only numeric characters but I have not gotten that far just yet. I just am trying to get it to recognize if the input does not have a number in it. If not it loops the error message until there is only numbers. After this I am trying to convert the string to double by use of atof() so it can be returned in main(). I get a debug assertion failed! Message upon runtime which says, expression string subscript out of range if a number is put in. Other wise if a letter has input, it keeps looping its self with the error message. I got my code for the function below. Any one have any clues as to what I am doing wrong? I am out of ideas...
double Bet::betProb(std::string b)
{
bool alphChar = false;
double doubleBet;
for(int i = 0; i < b.size(); i++){
if(isalpha(b[i])){
alphChar = true;
}
}
while(alphChar){
cout << "Error! Bet only with numbers." << endl;
cin >> b;
for(int i = 0; i < b.size(); i++){
if(!isalpha(b[i])){
alphChar = false;
}
}
}
string F=b;
int T=F.size();
char Change[100];
for (int a=0;a<=T;a++)
{
Change[a]=F[a];
}
doubleBet = atof(Change);
return doubleBet;
}

Since your problem has already been resolved, I thought I'd show you the way this would be done using standard C++ functionality:
#include <string>
#include <stdexcept>
double Bet::betProb(const std::string& str)
{
double d;
try {
d = std::stod(str);
} catch (const std::invalid_argument&) {
std::cerr << "Argument is invalid\n";
throw;
} catch (const std::out_of_range&) {
std::cerr << "Argument is out of range for a double\n";
throw;
}
return d;
}

Related

"Exception thrown: Write access violation in C++" [closed]

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 2 days ago.
Improve this question
I get this error when I try to compile the following code: Exception thrown: write access violation.
this was 0xF81EFA0000.
#include <iostream>
using namespace std;
class Demo
{
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main() {
Demo obj[4];
int i;
for (i = 0; 1 < 4; i++)
{
obj[i].setX(i);
}
for (i = 0; i < 4; i++)
{
cout << "obj[" << i << "].getX(): " <<
obj[i].getX() << endl;
}
// Textual Data Types
// Boolean
return 0;
}
The error concerns line 10, where "x = i" is written. Any help would be good.
I haven't tried anything to fix it, other than writing brackets after "x", which obviously didn't work. I expect this program to print an array of objects when working properly.

Error in C++ code: In function 'int main(int, char**)': [closed]

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 2 years ago.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am a newbie and a student of Computer Sciences. I am doing my assignment Which is spell checker of a text file. I have done the code but I am getting the following errors. I am unable to resolve it. kindly help me guys. Thanks!
Here is my code:
#include<iostream>
#include<fstream>
using namespace std;
class spell_check
{
private:
int line_number=0;
string input="" ;
string dictionary="";
bool condition=false;
public:
void process(int x,char *y[]);
};
void spell_check::process(int x,char *y[])
{
ifstream input_file;
input_file.open(y[2]);
ofstream output_file;
output_file.open(y[4]);
while(!input_file.eof())
{
line_number++;
getline(input_file,input);
ifstream dictionary_file("dictionary.txt");
while(!dictionary_file.eof())
{
getline(dictionary_file,dictionary);
if( input.compare(dictionary) == 0 )
{
condition=true;
break;
}
}
if(condition==false)
{
output_file<<"**Spell mistake** "<< "( " << input << ")"<< "[" <<"at line no: " << line_number <<"]"<<endl;
}
dictionary_file.close();
condition=false;
}
cout<<"Successfully Write "<<endl;
input_file.close();
output_file.close();
}
int main(int argum,char *argu_array[])
{
spell_check SC;
SC.process(argum, *argu_array);
return 0;
}
there is an error here:
int main(int argum,char *argu_array[])
{
spell_check SC;
SC.process(argum, *argu_array);
return 0;
}
since the type of the second argument of the main function is char* []
and also the type of the argument of the process method is the same:
void spell_check::process(int x,char *y[])
you don't have to dereference it, try this:
SC.process(argum, argu_array);

Unknown error in c++ program [closed]

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 6 years ago.
Improve this question
I have a c++ program which opens an url depending in what the user inputs.
Here's the code:
#include <iostream>
#include <string>
using namespace std;
int main(){
int i = 1;
string google = "https://www.google.com/search?q=";
string input;
getline(cin, input);
string changeSpace(string input)
{
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
text[i] = '+';
}
return text;
}
input = changeSpace(input);
cout << input << endl;
string url = string(google + input);
system(string("start " + url).c_str());
cout << url << endl;
}
The error is here:
string changeSpace(string input)
{
In the bracket it says it expected a " ; "
And I don't know why ocurrs that error, it may be a simple mistake, but I don't know it.
Please help me.
Your problem is because you're trying to define a function inside another function. You cannot do that.
Since C++11, the most similar thing you can do is using lambda.
int main() {
// stuff...
auto changeSpace = [] (string text) -> string
{
for (int i = 0; i < text.length(); i++)
{
if (text[i] == ' ')
text[i] = '+';
}
return text;
}
input = changeSpace(input);
// stuff...
}
But I bet that is not the only error in your code.
The Nesting of functions is not allowed in c++. Refer this: C++ can we have functions inside functions?
For using system(string("start " + url).c_str()); in your code you should include <cstdlib>. And also use return statement in main :return 0

why am I getting random results when incrementing an int [closed]

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 have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.

Verify if two strings are equal in C++ [closed]

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 years ago.
Improve this question
I'm doing an assignment that requires me to use a function to check whether two strings are equal. I keep getting a parse error on line 20, where the function is called, and I don't know what's wrong. Please take a look and let me know if you see what could be causing the problem. Thanks!
#include <iostream>
#include <string>
using namespace std;
bool checker(string firstWordParameter, string secondWordParameter);
int main()
{
string firstWord, secondWord;
bool match;
cout << "Hello user.\n"
<< "This program will determine whether two words are the same.\n"
<< "Please enter your first word you would like to check: ";
getline(cin, firstWord);
cout << "Great, now enter the second word: ";
getline(cin, secondWord);
match = bool checker(firstWord, secondWord);
if(match == true){
cout << "Match.";
}else{
cout << "Totally not a match.";
}
return 0;
}
bool checker(string firstWordParameter, string secondWordParameter)
{
if(firstWordParameter == secondWordParameter){
return true;
}else{
return false;
}
}
Try changing
match = bool checker(firstWord, secondWord);
into
match = checker(firstWord, secondWord);
Line 20 is
match = bool checker(firstWord, secondWord);
Change it to
match = checker(firstWord, secondWord);
Also when you see error in compiler, double click it then it will show you the line with the error.