Student needs help..please

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 8
Reputation: jamdownian is an unknown quantity at this point 
Solved Threads: 0
jamdownian jamdownian is offline Offline
Newbie Poster

Student needs help..please

 
0
  #1
Oct 18th, 2009
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

  1. #include"stdafx.h"
  2. #include<string>
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. class vowels
  7. {
  8. public:
  9. vowels();void getdata();void processdata();void display();string input;
  10. private:
  11. int a;int e;int i; int o; int u; int y;
  12. };
  13. vowels::vowels ()
  14. {
  15. int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input="";
  16. }
  17. void vowels::getdata()
  18. {
  19. cout<<"Enter String of Characters->>";
  20. getline(cin,input);
  21. }
  22. void vowels::processdata()
  23. {
  24. if(input=="A" && input=="a")
  25. {
  26. a=a++;
  27. }
  28. else if(input=="E" && input=="e")
  29. {
  30. e=e++;
  31. }
  32. else if(input=="I" && input=="i")
  33. {
  34. i=i++;
  35. }
  36. else if(input=="O" && input=="o")
  37. {
  38. o=o++;
  39. }
  40. else if(input=="U" && input=="u")
  41. {
  42. u=u++;
  43. }
  44. }
  45. void vowels::display()
  46. {
  47. printf("Vowels present in the typed string are:\n");
  48. cout<<"A="<<a<<endl;
  49. cout<<"E="<<e<<endl;
  50. cout<<"I="<<i<<endl;
  51. cout<<"O="<<o<<endl;
  52. cout<<"U="<<u<<endl;
  53. }
  54. int main()
  55. {
  56. vowels countvowels;
  57. countvowels.getdata();
  58. countvowels.processdata();
  59. countvowels.display();
  60. system("pause");
  61. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: jamdownian is an unknown quantity at this point 
Solved Threads: 0
jamdownian jamdownian is offline Offline
Newbie Poster
 
0
  #2
Oct 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: jamdownian is an unknown quantity at this point 
Solved Threads: 0
jamdownian jamdownian is offline Offline
Newbie Poster
 
0
  #3
Oct 18th, 2009
no help at all...but thanks anyway. Im gonna try somewhere else.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training
 
0
  #4
Oct 18th, 2009
You need to pay more attention in your classes.Very silly mistakes.

1)
  1. vowels::vowels ()
  2. {
  3. int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input="";
  4. }
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)
  1. 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".
Last edited by csurfer; Oct 18th, 2009 at 2:30 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,619
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess
 
0
  #5
Oct 18th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: jamdownian is an unknown quantity at this point 
Solved Threads: 0
jamdownian jamdownian is offline Offline
Newbie Poster
 
0
  #6
Oct 18th, 2009
Originally Posted by csurfer View Post
You need to pay more attention in your classes.Very silly mistakes.

1)
  1. vowels::vowels ()
  2. {
  3. int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input="";
  4. }
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)
  1. 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])
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: jamdownian is an unknown quantity at this point 
Solved Threads: 0
jamdownian jamdownian is offline Offline
Newbie Poster
 
0
  #7
Oct 18th, 2009
Originally Posted by Narue View Post
>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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training
 
0
  #8
Oct 18th, 2009
Originally Posted by jamdownian View Post
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.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: jamdownian is an unknown quantity at this point 
Solved Threads: 0
jamdownian jamdownian is offline Offline
Newbie Poster
 
0
  #9
Oct 18th, 2009
Originally Posted by csurfer View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training
 
0
  #10
Oct 18th, 2009
1)
  1. if(input=="A" && input=="a")
  2. //Same applies to all other vowel statements also
2)
  1. a=a++;
  2. //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).
I Surf in "C"....
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC