98% done

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2005
Posts: 31
Reputation: sahil_logic is an unknown quantity at this point 
Solved Threads: 0
sahil_logic sahil_logic is offline Offline
Light Poster

98% done

 
0
  #1
Nov 22nd, 2005
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4.  
  5. int main()
  6. { void palcheck(char str[]);
  7. char ans;
  8. char str[20];
  9. do
  10. {
  11.  
  12. cout<<"Enter the string to check if it is a palindrome. ";
  13. gets(str);
  14. palcheck(str);
  15. cout<<"\nEnter Y to continue. ";
  16. cin>>ans;
  17. }while (ans=='y'||ans=='Y');
  18.  
  19.  
  20. }
  21. void palcheck(char str[])
  22. { int l=0,k=0,flag;
  23. for(int i=0;str[i]!='\0';i++)
  24. l=l+1;
  25. k=l;
  26. for(int j=0;j<l/2;j++)
  27. {
  28. k--;
  29. if(str[j]==str[k])
  30. flag=1;
  31. else
  32. {flag=0;
  33. break;}
  34. }
  35. if(flag==0)
  36. cout<<"\nString is not a palindrome. ";
  37. else cout<<"String is a palindrome. ";
  38. }
i am almost done . just a small error. it is not asking for new input which i want it to do . it is storing the previous value. some problem around the do loop but not able to figure out. i think it has to do something with declaration of variable.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: 98% done

 
0
  #2
Nov 22nd, 2005
void palcheck(char str[]);
needs to be written before main, not inside of it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: 98% done

 
0
  #3
Nov 24th, 2005
Originally Posted by sahil_logic
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4.  
  5. int main()
  6. { void palcheck(char str[]);
  7. char ans;
  8. char str[20];
  9. do
  10. {
  11.  
  12. cout<<"Enter the string to check if it is a palindrome. ";
  13. gets(str);
  14. palcheck(str);
  15. cout<<"\nEnter Y to continue. ";
  16. cin>>ans;
  17. }while (ans=='y'||ans=='Y');
  18.  
  19.  
  20. }
  21. void palcheck(char str[])
  22. { int l=0,k=0,flag;
  23. for(int i=0;str[i]!='\0';i++)
  24. l=l+1;
  25. k=l;
  26. for(int j=0;j<l/2;j++)
  27. {
  28. k--;
  29. if(str[j]==str[k])
  30. flag=1;
  31. else
  32. {flag=0;
  33. break;}
  34. }
  35. if(flag==0)
  36. cout<<"\nString is not a palindrome. ";
  37. else cout<<"String is a palindrome. ";
  38. }
i am almost done . just a small error. it is not asking for new input which i want it to do . it is storing the previous value. some problem around the do loop but not able to figure out. i think it has to do something with declaration of variable.
Argh, stop with the school work already... And dont use gets, se man gets... Well the code looks not as borked as I first thought, but its pretty damn borked anyway I just dont have gdb installed so I'm not sure I can pinpoint the exact reason... However a educated quess is that the cin >> only eats the 'y' and not the following \n getting stuck in the gets, it does not explain why the same value should occur more than once though since that should output a zero length string, could be some odd thing happening with the buffering when both new and old io is used (not so on my compiler though)
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: 98% done

 
0
  #4
Nov 24th, 2005
  1. Don't use gets(). It's dangerous. (Buffer overruns.)
  2. In C++, <stdio.h> becomes <cstdio>.
  3. <conio.h> is non-standard. You're not using it anywhere.
  4. <iostream.h> is also deprecated. Use
    1. #include <iostream>
    2. using namespace std;
    instead.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: 98% done

 
0
  #5
Nov 24th, 2005
You need to clear the input buffer after the gets. (As I said, you shouldn't even use gets(). Use getline instead (in C++, fgets() in C).)
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: 98% done

 
0
  #6
Nov 24th, 2005
void palcheck(char str[]);
needs to be written before main, not inside of it.
Wrong. It can go inside main, but like a variable, it will only be visible in main().
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC