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
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 4 years ago.
Improve this question
I'm a student who just learned to use C++ for a few weeks. I write C++ code on Code:: Block and I am trying to run this code to learn about vector:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> intVector;
for(int i = 0; i < 10; i++)
{
intVector.push_back(i+1);
}
cout << "Numbers in vector: ";
for(int i = 0; i < intVector.size(); i++)
{
cout << intVector[i] + " ";
}
}
But the output is really weird:
Numbers in vector: vector::_M_emplace_back_auxector::_M_emplace_back_auxctor::_M_emplace_back_auxtor::_M_emplace_back_auxor::_M_emplace_back_auxr::_M_emplace_back_aux::_M_emplace_back_aux:_M_emplace_back_aux_M_emplace_back_aux
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.
Does anyone know how to fix this problem? Do I use vector in the wrong way?
Try changing the cout line like this:
cout << intVector[i] << " ";
What you're trying to do is add an int& and a string literal -- in some compilers it will emit a warning indicating for you that you're not getting what you intended for.
For example with Clang:
warning: adding '__gnu_cxx::__alloc_traits>::value_type' (aka 'int') to a string does not append to the string [-Wstring-plus-int]
cout << intVector[i] + " ";
~~~~~~~~~~~~~^~~~~
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
i was extracting lowercase and uppercase characters from a string . then print those uppercase and lowercase string in sorted order in .to sort the string i used std::sort function .but it's not working.
here is my code
#include <bits/stdc++.h>
using namespace std;
int main() {
//std::ios::sync_with_stdio(false);
char str[1005];
char low[1005];
char upr[1005];
int n;
int t;
cin>>t;
while(t--)
{
cin>>n;
cin>>str;
low[0]='\0';
upr[0]='\0';
int i=0,j=0,k=0;
while(i<n)
{
(str[i]>='A' && str[i]<='Z') ? (upr[j]=str[i],++j) : (low[k]=str[i],++k) ;
++i;
}
low[j]='\0';
upr[k]='\0';
cout<<"lowercase="<<low<<'\n';
cout<<"uppercase="<<upr<<'\n';
sort(low,low+j);
sort(upr,upr+k);
cout<<"lowercase="<<low<<'\n';
cout<<"uppercase="<<upr<<'\n';
}
return 0;
}
test case:
1 // number of test cases
15 // length of string
abHJUdjKIpwlaKm
output:
lowercase=abdjpw //before sorting
uppercase=HJUKIK //before sorting
lowercase=abdjpw //after sorting
uppercase= //after sorting
after sorting uppercase string don't even print.
You have a bug with indexes, fix:
low[k] = '\0';
upr[j] = '\0';
cout << "lowercase=" << low << '\n';
cout << "uppercase=" << upr << '\n';
sort(low, low + k);
sort(upr, upr + j);
cout << "lowercase=" << low << '\n';
cout << "uppercase=" << upr << '\n';
Exchanged k and j in this snippet.
Better variable names would help. Try replacing j and k with something more descriptive like lowIndex and uprIndex. Then you should see the problem.
I noticed you were using j variable for uppercase and k for lowercase in the while loop then proceeded to do the opposite later. Was this intentional? Wondering if that's causing a bug.
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
As soon as I enter the string and press enter the compiler shows debug error and says abort() was encountered.
What is actually wrong?
#include<iostream>
#include<string>
using namespace std;
int main()
{
std::string str;
std::string rev;
std::cout << "Enter the string\n";
std::getline(std::cin, str);
int len = str.size();
for (int i = len; i > 0; i--)
{
std::string temp;
temp= str.at(i);
int j = 1;
rev.insert(j, temp);
j++;
}
std::cout << "The reversed string is\n";
std::cout << rev;
cout << "Thank You";
cin.get();
}
for (int i = len; i > 0; i--)
Should be
for (size_t i = len - 1; i >= 0; i--)
// ^^^ ^
The statement
temp= str.at(i);
will be out of bounds for the first iteration otherwise.
Indices in c++ are in the [0 ... (size - 1)]range.
The earlier answer points out the problems with str.at (i). There's another: on the first iteration it calls rev.insert (1, ...). Since rev has, at that point, a length of zero, that is an out-of-bound access, which will cause an out_of_range exception to be thrown, terminating your program.
Also, move the declaration of j out of the loop. Now it gets recreated with value 1 each time through.
Using an entire string object for tmp seems overkill. Indeed, rev += str.at(i); could replace the entire loop body.
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.
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;
}