| | |
Student needs help..please
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
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
Built with Visual Studio 2008
C++ Syntax (Toggle Plain Text)
#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"); }
0
#4 Oct 18th, 2009
You need to pay more attention in your classes.Very silly mistakes.
1)
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 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".
1)
c++ Syntax (Toggle Plain Text)
vowels::vowels () { int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input=""; }
2)
c++ Syntax (Toggle Plain Text)
if(input=="A" && input=="a")
Last edited by csurfer; Oct 18th, 2009 at 2:30 pm.
I Surf in "C"....
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.
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.
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#6 Oct 18th, 2009
•
•
•
•
You need to pay more attention in your classes.Very silly mistakes.
1)
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.c++ Syntax (Toggle Plain Text)
vowels::vowels () { int a=0; int e=0; int i=0; int o=0; int u=0; int y=0;input=""; }
2)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".c++ Syntax (Toggle Plain Text)
if(input=="A" && input=="a")
Click link for picture(i didnt see any[image] [/image])
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#7 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.
0
#8 Oct 18th, 2009
•
•
•
•
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.
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"....
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
0
#9 Oct 18th, 2009
•
•
•
•
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.
0
#10 Oct 18th, 2009
1) 2)
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
c++ Syntax (Toggle Plain Text)
if(input=="A" && input=="a") //Same applies to all other vowel statements also
c++ Syntax (Toggle Plain Text)
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). I Surf in "C"....
![]() |
Similar Threads
- calculate the grades for a student using if..else statements. (Java)
- Student Suspended for using PHP!! (IT Professionals' Lounge)
- JAVA program help (Student/Grades) (Java)
- Who can help a student with her exams in Java?? (Java)
- Quiet student (Computer Science)
- Querying database records (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: help with a number array
- Next Thread: Student, help with fstream
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






