have a string in c++, need help to split it [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
#include<iostream>
#include<string>
#include<ctype.h>
#include<vector>
using namespace std;
int main(){
vector<string>temp;
int m = 0;
string strOperand = "10 7 5.0 * -";
string strCurData = "";
for (int i = 0; i < strOperand.size(); i++)
{
if (isdigit(strOperand[i]) || strOperand[i] == '.')
{
strCurData.push_back(strOperand[i]);
}
else
if (!isdigit(strOperand[i]) && strOperand[i] != '.'){
if (strOperand[i] == ' ')
{
temp[m] = strCurData;
strCurData = "";
m++;
}
else
if (strOperand[i] == '+' || strOperand[i] == '-' || strOperand[i] == '*' || strOperand[i] == '/' || strOperand[i] == '%' || strOperand[i] == '^')
{
temp[m] = strOperand[i];
m++;
}
else
if (strOperand[i]=='\0')
{
break;
}
}
}
cout << m;
}
i want to split the string to a vector, but it turns to be a error warning that vector subscript is out of range, i am really worry about this, can someone please help me, thanks!

temp is empty, yet you access its nonexistent elements by index. The behavior you describe is as expected.

Related

If statement still going through when my condition should be false [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 1 year ago.
Improve this question
I am attempting to write a Caesar encryption program. I have written two functions.
The first (sanitize) allows me to make sure that all strings are fully capitalized, here is the source code.
string sanitize(string message) {
for(int i; i < message.length(); i++){
message[i] = toupper(message[i]);
}
return message;
}
The second (caesar) encrypts the message given. Here is the source code for that as well.
string caesar(string c_message, char direction) {
if (direction = 'R') {
for(int j; j < c_message.length(); j++) {
if((int)c_message[j] + 3 > 90) {
c_message[j] = (char)(64 + (3 - (90 - (int)c_message[j])));
} else {
c_message[j] = (char)((int)c_message[j] + 3);
}
}
} else if (direction = 'L') {
for(int i; i < c_message.length(); i++) {
if((int)c_message[i] - 3 < 65) {
c_message[i] = (char)(91 - (3 - ((int)c_message[i] - 65)));
} else {
c_message[i] = (char)((int)c_message[i] - 3);
}
}
} else {
cout << "directions: 'L' or 'R'" << endl;
}
return c_message;
}
An example of execution :
int main(){
cout << sanitize("HELLO") << " " << (char)3 << endl;
cout << caesar("HELLO", 'L') << endl;
return 0;
}
The first if statement works, but the second does not.
if (direction = 'R') {
} else if (direction = 'L') {
These lines are wrong. = in C++ is an assignment operator and it sets the value of a variable in lefthand to the value of righthand. Then, it is evaluated to the new (righthand) value. Another point is that nonzero values are considered as true when used as condition.
You should use a comparision operator == instead of that like this:
if (direction == 'R') {
} else if (direction == 'L') {

Getting an error while reversing vowels in a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 3 years ago.
Improve this question
I know there are similar questions about this topic but I wanted to know error in my approach.
I'm writing a code to reverse vowels in a string. I first took all the vowels of a string into a vector and then I looped the string from backwards by replacing the vowels but I keep getting an error.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
string reverseVowels(string s)
{
vector<char>v;
vector<char>v2;
char c;
for (int i = 0; i < s.size(); i++)
{
//taking all the vowels of the string into a vector
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
v.push_back(s[i]);
}
}
//reversing the vowels of the string
for (int i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
s[i] = v[i]; //Getting an error here
}
}
return s;
}
int main()
{
//Required output is "holle"
string s = "hello";
string p = reverseVowels(s);
cout << p << endl;
return 0;
}
You need another index for the reversed vowels:
for (int vowelIdx = 0, i = s.size() - 1; i >= 0; i--)
{
if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')
{
s[i] = v[vowelIdx++]; //Getting an error here
}
}
edit
Regarding your comment below; let's examine the code for the word hello. The first loop runs through all the vowels in hello and fills the vector v as eo. It has two vowels with e as the first and o as the second/last element.
Your second loop reverse traverses the word hello, and encounters the first vowel o. You need to reset this to e which is the first element of the vector v. This is where your code get an error. The value of i is 4 now, hence you are trying to use the 4.th index of vector v which has only two elements. You have to set s[4] = v[0] and s[1] = v[1]. However, your code tries to set s[4] = v[4] (see the error?) and s[1] = v[1].
Hope it's clear now, if not try it on paper with a longer word. I'm sure you will see the point.

C++ error returning a string from a function [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 4 years ago.
Improve this question
I'm trying to return a string from the function solution() but I am getting the error below. Apologies if this is pretty basic but could anybody explain how to return the string. I understand that it is related to pointers.
error: could not convert ‘(std::__cxx11::string*)(& hexaDeciNum)’ from
‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ to
‘std::__cxx11::string {aka std::__cxx11::basic_string}’
string solution(string &S){
int n = stoi(S);
int answer = 0;
// char array to store hexadecimal number
string hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j].compare("A") ==0 or hexaDeciNum[j].compare("B") ==0 or hexaDeciNum[j].compare("C") ==0 or hexaDeciNum[j].compare("D") ==0 or hexaDeciNum[j].compare("E") ==0 or hexaDeciNum[j].compare("F") ==0 or hexaDeciNum[j].compare("1") ==0 or hexaDeciNum[j].compare("0") ==0 ) {
answer = 1;
}
}
if (answer == 1){
return hexaDeciNum;
}
else {
return "ERROR";
}
}
int main() {
string word = "257";
string answer = solution(word);
return 0;
}
hexaDeciNum is defined as string hexaDeciNum[100]. It is not a string - it is an array of 100 string instances.
You're attempting to return it from a function that should return string.
You should define hexaDeciNum as string hexaDeciNum; instead of string hexaDeciNum[100];. With that way, you can still indexing operator. However you can not use compare method anymore, because each element of string is a char. Instead use operator == like in the following for your piece of code.
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j] == 'A' or hexaDeciNum[j]=='B' or
hexaDeciNum[j] == 'C' or hexaDeciNum[j] == 'D' or
hexaDeciNum[j] == 'E' or hexaDeciNum[j] == 'F' or
hexaDeciNum[j] == '1' or hexaDeciNum[j] == '0' ) {
answer = 1;
}
}
and please don't forget to compile it for c++ 11 with -std=c++11 option of compiler.

There is an error in this program. Please can anyone identify it? Thanks [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
Hi all there seems to be an error in this program in the sense it is showing wrong output. Rather than showing number of consonants and vowels it is printing the length of the string.
#include<iostream>
using namespace std; // program to display the vowels and consonants of a string
int countvowels(char a[])
{
int count =0;
for(int i =0; a[i]!='\0';i++)
{
if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) // ignores digits,special characters
{
if((a[i]=65) || (a[i]=69) || (a[i]=73) || (a[i]=79) || (a[i]=85) || (a[i]=97) || (a[i]=101) || (a[i]=105) || (a[i]=111) || (a[i]=117) ) //ignores consonants
{
count++; //counts filtered out vowels
}
}
}
return count; // returns number of vowels which will be captured by x of main function
}
int countconsonants(char a[])
{
int count =0;
for(int i =0; a[i]!='\0';i++)
{
if((a[i]>=65 && a[i]<=90) || (a[i]>=97 && a[i]<=122)) // ignores digits,special characters
{
if((a[i]!=65) || (a[i]!=69) || (a[i]!=73) || (a[i]!=79) || (a[i]!=85) || (a[i]!=97) || (a[i]!=101) || (a[i]!=105) || (a[i]!=111) ||(a[i]!=117) ) //ignores vowels
{
count++; //counts filtered out consonants
}
}
}
return count; // returns number of consonants which will be captured by y of
main function
}
int main()
{
char a[100];
cout<<"Enter the string"<<endl;cin.get(a,100);
int x = countvowels(a);
int y = countconsonants(a);
cout<<"Number of vowels is"<<x<<endl; //nothing much to say about this part of the program. x just displays it and y does the same
cout<<"Number of consonants is"<<y<<endl;
return 0;
}
Here are the ASCII values of vowels.
A , a = 65,97
E,e = 69, 101
I,i = 73, 105
O,o = 79,111
U,u = 85,117
You have two problems with your code:
In checking for equality. To check if two value are equal you should use double equals== instead of a single equals. A single equal sign will mean assignment not equality check
In logical condition in count_consonants. a != 1 || a != 2 will always evaluate to true

Bug in if statement [duplicate]

This question already has answers here:
C++ if else if not working properly [duplicate]
(5 answers)
Closed 5 years ago.
No matter what string I add to s[i], I I still get a "yes" as an output while if I remove ||(OR) it works perfectly.
for(int i=0; i<T; i++)
{
if(s[i]=="ccc"||"ccs")
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
This is how it's written:
if(s[i] == "ccc" || s[i] == "ccs")
Change your if condition to:
if(s[i] == "ccc" || s[i] == "ccs")