944,111 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 779
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 18th, 2009
0

Student needs help..please

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamdownian is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student needs help..please
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamdownian is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student needs help..please
no help at all...but thanks anyway. Im gonna try somewhere else.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamdownian is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student needs help..please
You need to pay more attention in your classes.Very silly mistakes.

1)
c++ Syntax (Toggle Plain Text)
  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)
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Oct 18th, 2009
2
Re: Student needs help..please
>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 18th, 2009
0
Re: Student needs help..please
Click to Expand / Collapse  Quote originally posted by csurfer ...
You need to pay more attention in your classes.Very silly mistakes.

1)
c++ Syntax (Toggle Plain Text)
  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)
c++ Syntax (Toggle Plain Text)
  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])
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamdownian is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student needs help..please
Click to Expand / Collapse  Quote originally posted by Narue ...
>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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamdownian is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student needs help..please
Click to Expand / Collapse  Quote originally posted by jamdownian ...
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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Oct 18th, 2009
0
Re: Student needs help..please
Click to Expand / Collapse  Quote originally posted by csurfer ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamdownian is offline Offline
11 posts
since Oct 2009
Oct 18th, 2009
0
Re: Student needs help..please
1)
c++ Syntax (Toggle Plain Text)
  1. if(input=="A" && input=="a")
  2. //Same applies to all other vowel statements also
2)
c++ Syntax (Toggle Plain Text)
  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).
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with a number array
Next Thread in C++ Forum Timeline: Student, help with fstream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC