954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Student needs help..please

I've written a C++ program that compiles successfully but is give me some logic error. It's giving me some negative numbers can someone please run the code and see what ive done wrong.

Built with Visual Studio 2008

#include"stdafx.h"
#include<string>
#include<iostream>
using namespace std;

class vowels
{
public:
       vowels();void getdata();void processdata();void display();string input;
private:
		int a;int e;int i; int o; int u; int y;
};
vowels::vowels ()
{
	int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input="";
}
void vowels::getdata()
{
	cout<<"Enter String of Characters->>";
	getline(cin,input);
}
void vowels::processdata()
{
if(input=="A" && input=="a")
{
a=a++;
}
else if(input=="E" && input=="e")
{
e=e++;
}
else if(input=="I" && input=="i")
{
i=i++;
}
else if(input=="O" && input=="o")
{
o=o++;
}
else if(input=="U" && input=="u")
{
u=u++;
}
}
void vowels::display()
{
printf("Vowels present in the typed string are:\n");
cout<<"A="<<a<<endl;
cout<<"E="<<e<<endl;
cout<<"I="<<i<<endl;
cout<<"O="<<o<<endl;
cout<<"U="<<u<<endl;
}
int main()
{
	vowels countvowels;
	countvowels.getdata();
	countvowels.processdata();
	countvowels.display();
system("pause");
}
jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

And the code's purpose is to count number of vowels in a string. In this order:
A
E
I
O
U
with constructor with outside class function definition.

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

no help at all...but thanks anyway. Im gonna try somewhere else.

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

You need to pay more attention in your classes.Very silly mistakes.

1)

vowels::vowels ()
{
	int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input="";
}

Class functions are used to manipulate the variables declared within the class.Here by declaring the variables again you are just removing the whole sense out of it.You just need to initialise the variables declared within the class.2)

if(input=="A" && input=="a")

If input equals 'A' how in the world can it be equal to 'a' at the same time....??? And && requires both the condition to be true.What you really need here is the"||" "or" and not "&&" "and".

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

>no help at all...but thanks anyway.
Your capacity for patience is staggering. I mean, you couldn't even wait a full hour when it's either SUNDAY, or the wee hours of Monday all over the world. Good luck passing your class, and succeeding at a programming career, because you'll clearly need it. :icon_rolleyes:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

You need to pay more attention in your classes.Very silly mistakes.

1)

vowels::vowels ()
{
	int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input="";
}

Class functions are used to manipulate the variables declared within the class.Here by declaring the variables again you are just removing the whole sense out of it.You just need to initialise the variables declared within the class.2)

if(input=="A" && input=="a")

If input equals 'A' how in the world can it be equal to 'a' at the same time....??? And && requires both the condition to be true.What you really need here is the"||" "or" and not "&&" "and".

Thanks for the help but im getting these answers below. Click link for picture(i didnt see any[image] [/image])

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
>no help at all...but thanks anyway. Your capacity for patience is staggering. I mean, you couldn't even wait a full hour when it's either SUNDAY, or the wee hours of Monday all over the world. Good luck passing your class, and succeeding at a programming career, because you'll clearly need it. :icon_rolleyes:

Ive been reading alot of threads and im getting the impression that students asking for help and getting it is like a taboo. "NO homework allowed".... but now i see that there are people on this forum that will help and when i get the a better level in programming i will be willing to help.

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 
Ive been reading alot of threads and im getting the impression that students asking for help and getting it is like a taboo. "NO homework allowed".... but now i see that there are people on this forum that will help and when i get the a better level in programming i will be willing to help.

You have a very wrong impression about DANIWEB and the culture here... We don't say "No Home Work" we say "We wont help until you put in some effort",and even you would agree that we are not wrong.

We are here to learn and to help others learn. And none here are workless and come here for fun. Everyone is really busy but take some time out to help others who want to learn, so have a bit of patience.

And ya NARUE was the one who guided me when I was new and helped me a lot and DANIWEB is the reason for what I am.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

You have a very wrong impression about DANIWEB and the culture here... We don't say "No Home Work" we say "We wont help until you put in some effort",and even you would agree that we are not wrong.

We are here to learn and to help others learn. And none here are workless and come here for fun. Everyone is really busy but take some time out to help others who want to learn, so have a bit of patience.

And ya NARUE was the one who guided me when I was new and helped me a lot and DANIWEB is the reason for what I am.


thanks for re-assuring that Dani is a ace site. But i hope im not too much of a bother. Did you see the pic...? Its giving negative numbers.

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

1)

if(input=="A" && input=="a")
//Same applies to all other vowel statements also


2)

a=a++;
//Same applies to all other vowel statements also


The two most foolish statements in your code are the ones shown above.

1) What you really want to do is to compare every character of string input with character 'A' and 'a' . But you are taking the whole "input" string and comparing it with string "a" and "A". That means you will never get anything more than 0.

2) This is a bit complicated concept called sequence points .In case you want to know you can read it.

Generally when we try to manipulate the same variable more than once within a sequence point the result is always compiler dependent.It can be anything.Moreover here a++ alone efficiently replace the whole statement (2).

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

thanks..i havent sloved it but i gonna get some help form my pal.

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

As a general note if you want people to read your code learn to format it in a clean way. Having bad indents and no white space and cramming everything onto one line by throwing in semi-colons makes it just as hard to read if you don't use code tags and in the end when you compile this cutting out lines just makes it harder for you to go back to and fix up and will not make your file size any smaller.

I commented a few lines telling you what I did.

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

class vowels
{
	string input;
	int a, e, i, o, u, y;
	public:
	vowels();
	void getdata();
	void processdata();
	void display();
};
vowels::vowels()
{
	a = 0, e = 0, i = 0, o = 0, u = 0, y = 0; //made is so you dont redeclare the variables
	input = " ";
	getdata(); //when you make the variable class it auto starts the input instead of calling this function in main
}

void vowels::getdata()
{
	cout << "Enter String of Characters->> ";
	getline(cin,input);
	processdata(); //after input the vowel checker starts
	display(); //after vowel checker displays the data
}

void vowels::processdata()
{
	for( int x = 0; x < input.length()-1; x++ ) //added a for() loop for checking the string char by char not comparing the whole string
	{
		if( input[x] == 'A' || input[x] == 'a' ) //checks index 'x' and compares to a char not a string
		{
			a++; //a=a++; is exacly the same as saying a = a+1; and is the same as putting a++;
		}
		else if( input[x] == 'E' || input[x] == 'e' )
		{
			e++;
		}
		else if( input[x] == 'I' || input[x] == 'i' )
		{
			i++;
		}
		else if( input[x] == 'O' || input[x] == 'o' )
		{
			o++;
		}
		else if( input[x] == 'U' || input[x] == 'u' )
		{
			u++;
		}
	}
}
void vowels::display()
{
	printf("Vowels present in the typed string are:\n");
	cout << "A=" << a << endl;
	cout << "E=" << e << endl;
	cout << "I=" << i << endl;
	cout << "O=" << o << endl;
	cout << "U=" << u << endl;
}
int main()
{
	vowels countvowels; //everything is called within the class after this is declared

	system("pause");
	return 0;
}
sfuo
Practically a Master Poster
656 posts since Jul 2009
Reputation Points: 164
Solved Threads: 99
 

Thanks for the help,i was actually food fed with a spoon. Im very grateful..i guess i need to practice harder.

jamdownian
Newbie Poster
11 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You