Last week I posted a question about this same problem thinking that I needed to do it one way, when in fact I need to somehow use ASCII.
I learned from one of my classmates that I need to somehow use ASCII to read a series a 1's and 0's. I have to use a for loop so that I can have the program read some 1's and 0's and then tell me how many of those 1's and 0's are 1's. For example, the program should be able to read 1011001 and tell me that there are four 1's.
Also if you happen to write some example code, please write it as though the standard namespace is already being used. It makes it easier for me to read and make sense of. Please also explain what the code is supposed to do so that I can understand what it is that is going on.
Don't want to do the whole thing, but here's a start
1 2 3 4 5 6
string my_string("1011001");
for (int i = 0; i < my_string.length(); i++)
{
//prints the i-th element:
cout << my_string[i] << endl;
}
The first thing is your basic for-loop. If you are not sure of the syntax of that, I would look at the control flow tutorial.
The second thing uses the [] operator to access each character of the string, similar to how each element in a normal array is accessed.
You should be able to figure out how to count the zeroes and ones. Note that 0 is not the same thing as '0'
internally '1' is converted to the value of that character in current codepage and stored that way in variable. char is integral type. It only difference from ,say, int is its size (and that many functions are overloaded to threat chars differently).
So you do not need to replace anything. Compiler does this for you.
[Error] no match for 'operator==' (operand types are 'std::basic_string<char>' and 'char')
What am I doing wrong, here?
I have the libraries <iostream>, <string>, and <cmath> - the last one because I was hoping it would do something, but it didn't. Is there another that I need, maybe? I can't tell.
Unfortunately, it compiled and then gave an error when I went to execute. Something about <std::out_of_range> and then it says something about the fact that the problem is <basic_string::substr>.
What I've been asking about is a very small part of the whole program I need to complete. There is file input and output and an eof while loop that contains two for loops. This is what I have so far:
1)Indexing? Does that have to do with arrays? 'Cause I can't use those.
2)I just looked at my calcualteData1 file and found the problem it only had one character. I fixed that, but now it does not come out with the right nember of homeworks which would be five because the string is <1001111>.