Reading a series of 1's and 0's with ASCII

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.

Thanks in advance, Ike.
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'
Last edited on
need to somehow use ASCII
It is way harder to not use ASCII
For example, the program should be able to read 1011001 and tell me that there are four 1's.
1
2
3
4
5
6
int ones_count = 0;
char c;
while(cin >> c)
{
    if(c == '1') ++ones_count;
}
or
int ones_count = count(istream_iterator<char>(cin), istream_iterator<char>, '1');
Last edited on
MiiNiPaa

Do you replace the '1' with the ASCII for the number 1?
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.
I thought that was the case, however this is what I have:

const int NUMHOMEWORKS = 7;
homeworksum = 0;
for(numhomework = 0; numhomework < NUMHOMEWORKS - 1; numhomework++)
{
if(chHomework.substr(numhomework, numhomework) == '1')
++homeworksum;
}

//The chHomework string is 1001111.

I'm getting some type of error message saying:

[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.
I think you are trying to use ASCII Value instead of character '1'
Note: Character '1' is equal to Integer 49

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
    string bits="10010101";
    int count_1s = 0;
    
    for(int i=0;i<bits.size();i++)
    {
        if(bits[i] == 49)
            count_1s++;
    }
    
    cout << "Number Of 1's are: " << count_1s;
    cout << endl;
    cin.ignore();
    return 0;
}
What am I doing wrong, here?
substr returns a string. You cannot compare a string with a single char.
Thanks MiiNiPaa.

I used < "1" > and it compiled.
It is slow and not exactly straighforward. Why do you use subtstr? Why not just access elemnt by index?
1
2
if (chHomework[numhomework] == '1')
    //... 
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Adds up the grades for a number of students
from one file and outputs their final
grade in another file
*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
     ifstream inData;
     ofstream outData;
     const float MAXPROGRAMS = 400.0;
     const int NUMHOMEWORKS = 7;
     const float NUMPROGRAMS = 4;
     int  exam1, exam2, exam3;
     int finalexam, examtotal, examMin;
     int numhomework, homeworksum, finalgrade;
     int program, programsum, numprogram;
     string chHomework;
     char chGrade;
     
     inData.open("calculateData1.txt");
     if (not inData)
        {
          cout << "ERROR -- Could not open calculateData1" << endl;
          system ("pause");
          return 1;
        }
        
     outData.open("calculatedData1.txt");
     if (not outData)
        {
          cout << "ERROR -- Could not open calculatedData1" << endl;
          system ("pause");
          return 1;
        }
     
     //Starts off the outData file
     outData << setw(13) << setiosflags(ios::right) << "Final exam"
             << setw(16) << "Hourly Exams"
             << setw(12) << "Homework"
             << setw(16) << "Programs"
             << setw(20) << "Letter Grade"
             << setw(15) << "Final Grade" << endl;     
     
     //Starts calling values from the inData file
     inData >> finalexam;
     
     outData << setw(9) << setiosflags(ios::right) << finalexam;
     
     //eof-loop
     while(inData)
        {
          examtotal = 0;
     	  inData >> exam1 >> exam2 >> exam3;
     	  
     	  if(exam1 < exam2)
 	         examMin = exam1;
          else if(exam1 > exam2)
             examMin = exam2;
          else if(exam2 > exam3)
             examMin = exam3;
             
     	  examtotal = exam1 + exam2 + exam3 - examMin;
     	  
          outData << setw(9) << exam1
      		      << setw(6) << exam2
      		      << setw(6) << exam3;
          
		  //Adds up the homeworks               	  
     	  inData >> chHomework;
     	  homeworksum = 0; //This declaration is only used to add up the homeworks
     	  for(numhomework = 0; numhomework < NUMHOMEWORKS - 1; numhomework++)
     	    {
     	    	if(chHomework.substr(numhomework, numhomework) == '1')	
     	             ++homeworksum;
     	    }
          
          //Readies the homeworksum for output
	  	  if(homeworksum >= NUMHOMEWORKS)
	  	  	  --homeworksum;
	  	    
      	  outData << setw(7) << homeworksum << "    ";
     	  
     	  //Adds up the program grades
     	  programsum = 0;
     	  for(numprogram = 0;  numprogram < NUMPROGRAMS - 1; numprogram++)
     	    {
  	    	    inData >> program;
  	    	    
  	    	    outData << setw(5) << program;
  	    	    
  	    	    programsum = programsum + program;
     	    }
     	    
          outData << //I haven't gotten to this yet
                           	    
     	  inData >> finalexam;
     	  
     	  outData << setw(9) << finalexam;
        }
     
     
     
     system ("pause");
     return 0;
}


Please let me know if find the problem, because I sure as hell don't see it.
1) your substr does not do what you think. Use indexing instead, it is simple and fast.

2) If substr throws an exception, that means your string is less than 6 character long and you are trying to access unexisting element
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>.
Ike4948 wrote:
Indexing? Does that have to do with arrays? 'Cause I can't use those.
Too bad, because string is an array of characters intenally. std::string supports accessing individual characters through subscript operator.

Ike4948 wrote:
it does not come out with the right nember of homeworks
This is because of
MiiNiPaa wrote:
your substr does not do what you think
I get that, but what is it doing?

Edit: Never mind, I figured it out. The length of the substr was always different and so didn't do what I wanted it to. The length needed to be 1.
Last edited on
Topic archived. No new replies allowed.