College work

I have been trained in Java programing only during my first 2 years of college, but now I have this new teacher that requests all works to be done in c++. I think it is a very good language to work with and whenever I have sometime I'd really like to learn more about it. Cutting the chat-chat, I have been assigned this work, very simple as described but due to my lack of knowledge and time I have been unable to accomplish, so I was wondering: CAN YOU FOLKS HELP ME OUT? The work is the following:

Build a console program in c++ to:

a) read a 50 chars phase, including blank spaces;
b) count how many blank spaces there are on the phase;
c) count how often does letters "a" and "e" appear on the phrase;
d) count how often the same pair of letters do appear(example: "aa", "rr", etc) and what are they;
e) print the results from itens b, c and d.

I have come up with the following code, but getting lots of errors and having hard time to figure them out. I don't even know if my logic is correct.

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
#include "stdafx.h"
#include "iostream"
#include <string.h>
using namespace std;

int numWhites(char phrs[50])
{
int numW = 0;
//int phrsSize = phrs.length();
int phrsSize = strlen(phrs);
for(int i=0; i<phrsSize;i++)
if(phrs[i] == ' ') numW++;

return numW;

}

int numaorE(char phrs[50])
{
int numaorE = 0;
//int phrsSize = phrs.length();
int phrsSize = strlen(phrs);
for(int i=0; i<phrsSize;i++)
if((phrs[i] == 'a') | (phrs[i] == 'A')) numaorE++;

return numaorE;

}

int main()
{
char phrase;
char pairs[50];
char letters[] = "abcdefghijklmnopqrstuvxwyz";
int whites;
int aore;
int pairofLetters;

cout <<"\nWrite your phrase with 50 charecters:\n";
cin.getline(phrase, 50);


whites = numWhites(phrase);
aore = numaore(phrase);

cout <<"White spaces: " <<whites;
cout <<"\nOccurrences of a or e: " <<aore;

pairofLetters = 0;
int zw = strlen(letters);
int i;
int a;
int x;


int phrsSize = strlen(phrase);
for(x=0; x<zw; x++)
	for(i=0;i<phrsSize;i++)
		if (phrase[i] = letters[x])
			if(phrase[i] = phrase[i + 1]) 
			pairofLetters ++;
			for(a=i+1;a<phrsSize;a++)
				if (phrase[a] = letras[x])
					if(phrase[a] = phrase[a + 1])
						pairofLetters ++;
	
	if (pairofLetters != 0)
		cout <<"Pair of letters " <<letters[x] + letters[x] <<" found " <<pairofLetters <<" times";

	pairofLetters = 0;

	system("pause");

}



WOULD SOMEBODY PLEASE HELP THIS BROTHER OUT?

I appreciate your attentition to this.
T. San.
At first glance, your if() statements on 59, 60, 63, and 64 are assignments, not comparisons. Comparison is ==.

Post the errors you are getting.
Thanks for the note on the comparisons. I don't get no errors actually, I just don't get what is expected.

It I type "Boy aint I lucky" only "Boy" gets assigned to phrase variable. So I can not calculate the number of white spaces, etc. Only this would help me to get it done.
I'm on my way to an exam, so I didn't have time to check the whole thing out, but if you look at

1
2
3
char phrase;

cin.getline(phrase, 50);


Does this look like a problem? Why not use:
1
2

string phrase;


I'm not sure if you were trying to use an old C-style string with the char type, but since you are including <string>, just use the string type. (and I'm not sure if <string.h> still works, that's the old style header.

Last edited on
grcunning, you got it, I was able to do it using string. Youu were also right on the assumption that I tried it before with string and it did not work because I was using the old version of it(<string.h>). thanks a lot for the tip. Also a very special thanks to jsmith.

Best regards.
Topic archived. No new replies allowed.