0

So I have been searching, but couldn't exactly find a solution for the thing I want to do. Basically User input's a char like "asdasdasd" and I need to go thru each symbol to check if it's 'a', and need to stop the loop after it has reached the end of char. I got this far and it works, it output's the char symbols one by one, but in this case I can't stop the loop, I tried adding another char at the end and make it something specific like "." and use that to stop the loop but then I need to use char o[] which brakes the symbol by symbol thing going on there. Need help.. any input would be appreciated.

#include <iostream>
using namespace std;
int main(){
    char o;
    cout<<"input: "; cin>>o;

    while(o!= '\0'){
        cout<<o<<"\n";
        cin >> o;
    }
    return 0;
}
  • 4
    In C++ (and most other programming language), `"asdasdasd"` is a *string*, not a char. And `'a'` is a *character* (or char), and not a symbol. – Jonathan Wood Mar 15 '18 at 13:03
  • 1
    "asdasdasd" cannot be a char. It can be a std::string or an array of char for instance. – Benjamin Barrois Mar 15 '18 at 13:04
  • 1
    Where is the part of the code that compares the current character to "a"? – Andrew Lau Mar 15 '18 at 13:04
  • 1
    What's "end of char" is exactly? You mean "end of line"? Then you'd better use `getline` and then iterate through resulting string. Or, if you want to scan exactly one character at once, take a look at `getchar` function and compare it's result against `\n` character. – Denis Sheremet Mar 15 '18 at 13:05
  • I think he is trying to read char from input and check it - like `a` + `enter`, then `s` + `enter` etc. Maybe ask user for count of chars and use `for` loop. Another solution could be break loop after specified string (not only char). – wpater Mar 15 '18 at 13:07
  • Clarify the question with what input must stop the reading. Pressing enter or pressing `a`. Which? Note that you can't type `\0` there's no such thing. – acraig5075 Mar 15 '18 at 13:10
  • Actually operator >> (istream&, char&) is principally the same as operator >> (istream&, int&) (just another output value size) - it reads a string representation of a number from the input stream. Not a single character! – AndreyS Scherbakov Mar 15 '18 at 13:18

2 Answers2

1

When I understand your question correct you will input a string like "ahobansodfb" and search in there after a specific char? If yes, this is a little example:

#include <iostream>
using namespace std;

int main() {

    string input;
    char search;
    int findPos = 0, countOfSearchedChars = 0;
    cout << "input searched char: ";
    cin >> search;

    cout << "input string: ";
    cin >> input;

    while((findPos = input.find_first_of(search, findPos)) != string::npos){
        cout << "Find searched char on pos: " << findPos << endl;
        ++findPos;
        ++countOfSearchedChars;
    }

    if(countOfSearchedChars == 0)
        cout << "Char: " << search << " not found in: " << input << endl;
    else
        cout << "Char: " << search << " found: " << countOfSearchedChars << " time/s in string: " << input <<  endl;
}

Explanation for: while((findPos = input.find_first_of(search, findPos)) != string::npos)

input.find_first_of(search, findPos) find the first place where the searched char lies. When the char doesn't found it returns string::npos (18446744073709551615)

So we loop so long the return value is not string::npos: != string::npos

Edit to answer comment question:
Possibilities to iterate through a string:

std::string str = "aabaaba";
for(char& c : str) {
    if(c == 'a'){
         //it's a
    } else if(c == 'b') {
         //it's b
    }
}

std::string str = "abaaba;
for(std::string::iterator it = str.begin(); it != str.end(); ++it) {
    if(*it == 'a'){
         //it's a
    } else if(*it == 'b') {
         //it's b
    }
}

For every character in string

Morchul
  • 1,987
  • 1
  • 7
  • 21
  • As I understand your example takes the first symbol of char search and then searches for that same symbol? and ye you understood right, I need to search for the specific symbol and need to find all of them. – BloodyPeach Mar 15 '18 at 13:54
  • You need to find all of them or only the first one? – Morchul Mar 15 '18 at 13:55
  • I mixed up the things your code does work like what I wanted xd, and yes, I need all of them, so I wanted to create that loop, but don't know how to stop it how to know that it's reached the end of the string? – BloodyPeach Mar 15 '18 at 14:05
  • Damn, didn't expect so many responses so fast you are amazing ;D thou I'm not sure if I know how to use this for what I need to do, basically I need to make a stack and add 1 to it if I find an 'a' and remove it if I find 'b' and add 0 if b was first and remove 0 if 'a' was found after and there can be multiple 1 or 0 in it – BloodyPeach Mar 15 '18 at 14:13
  • I'm not sure I understand it correctly. When you have an input of abaababaabaaabbb the stack will be: 111100? – Morchul Mar 15 '18 at 14:16
  • is there an option where i could go thru each of the string's symbols and then just check if they are 'a' or 'b' ? – BloodyPeach Mar 15 '18 at 14:16
  • and no in your example it would be 1(found a) then stack would be emty cuz found b so I deleate the 1 then add 1(found a), add 1(found a), delete one 1(found b) stack now would have 1 in it etc... – BloodyPeach Mar 15 '18 at 14:18
  • I add to my answer some ways to iterate througt a string. I understand a little bit what you want to do but not 100% can you explain it a little bit more? – Morchul Mar 15 '18 at 14:25
  • for the first one it doesn't allow me to use that [Error] range-based 'for' loops are not allowed in C++98 mode, as for the secound one c not declared, and this all is getting complicated xd are there no easyer ways to do this ? ;d – BloodyPeach Mar 15 '18 at 14:30
  • When you use c++98 I don't know another way. In the second loop you can split it a little bit and create the iterator outside of loop to make it clearer. With c++11 you can use `#include ` to do it on another way. – Morchul Mar 15 '18 at 14:36
0

THANKS for all the answers!, you were big help, this code will do for me, going to go research what it does x.x

std::string str = "abaaba;
for(std::string::iterator it = str.begin(); it != str.end(); ++it) {
    if(*it == 'a'){
         //it's a
    } else if(*it == 'b') {
         //it's b
    }
}