943,813 Members | Top Members by Rank

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

why can't i convert please help

Expand Post »
its in the num=="-".


C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9.  
  10. cout <<"enter the problemset and number""\n";
  11. //problems represents name and numbers
  12. string problems;
  13. char quote;
  14. char num;
  15. string number;
  16.  
  17. //gather name
  18. if(cin.peek()=='"' || cin.peek() == '\'')
  19. {
  20. cin >>quote;
  21. getline(cin,problems,quote);
  22. }
  23. else
  24. {
  25. while (!isdigit(cin.peek()) && !isspace(cin.peek()))
  26. {
  27. (char)cin.peek();
  28. problems += cin.get();
  29. }
  30. }
  31.  
  32.  
  33. //gather problem numbers
  34. //cin >> num;
  35. int lastdigit=0;
  36. while(num != '\n'){
  37.  
  38. cin.get(num);
  39. // can't use cin >> ws since it would eat the newline
  40. /* while (cin.peek() != '\n' && isspace(cin.peek()))
  41.   {
  42.   cin.ignore();
  43.   }*/
  44. if(num==',')
  45. {
  46. if(isdigit(cin.peek()))
  47. {
  48. cin.get(num);
  49. cout<<"from comma"<<number<<"\n";
  50. }
  51.  
  52. }
  53. else if (num=='-')
  54. {
  55. if(isdigit(cin.peek()))
  56. {
  57. // remember num
  58.  
  59. int b=atoi(cin.get(num));
  60. for(int a=lastdigit+1; a<=b; a++)
  61. {
  62. num++;
  63. }
  64. }
  65. else if(cin.peek()=='\n')// must be a newline!
  66. {
  67. //
  68. break;
  69. }
  70. else if(isdigit(cin.peek()))
  71. {
  72.  
  73. }
  74. if(isdigit(num))
  75. {
  76. lastdigit=num;
  77. }
  78. number=number+num;
  79. }
  80.  
  81. cout<<"the final number is"<<number<<"\n";
  82.  
  83. // print them all out
  84.  
  85. return 0;
  86. }
Similar Threads
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 29th, 2009
0

Re: why can't i convert please help

this is code that won't convert.

int b=atoi(cin.get(num));
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 29th, 2009
1

Re: why can't i convert please help

1->atoi signature is this:

int atoi ( const char * str );

2-> read some more threads here to find out alternatives to atoi. You should avoid using it.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jun 29th, 2009
0

Re: why can't i convert please help

k thanks.
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 29th, 2009
0

Re: why can't i convert please help

still confused why it won't convert.
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 29th, 2009
0

Re: why can't i convert please help

b=atoi(num,num.c_str());

it gives me error that request for member'c_str' in'num', which is of non-class type 'char'

what does that mean, i am passsing in a character
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 29th, 2009
0

Re: why can't i convert please help

Hmmm.. don't know where to start from. First things first, 'num' is of type 'char', do you have a class 'char' which has a fn c_str defined? No. Its a fn of the 'string' class. Then you didn't even read my earlier post properly.
I guess you need to read up the chapters on the 'data types' a couple of times over. Read about int,char,double etc. Then read about arrays, chars and pointers. And then about the 'string' class. And then come back to this problem again.
Last edited by Agni; Jun 29th, 2009 at 1:48 am.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jun 29th, 2009
0

Re: why can't i convert please help

i am supposed to this without pointers. The next topic is pointers.
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Jun 29th, 2009
0

Re: why can't i convert please help

Why don't you post the problem statement first. Then explain how you are trying to achieve it in your code. Then as I said, read about data-types. how did you find out about c_str fn? And I posted the correct function signature for atoi in my post above, does it have 2 parameters? Did you even read that?
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jun 29th, 2009
0

Re: why can't i convert please help

they enter a problem set name and number. I am supposed to get in my problem name which i can do fine. for the numbers though i need to have it print out all the numbers they are supposed to do.

Example
input:
L1,2,3-5
output:
do problems 1,2,3,4,5 of L.
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 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: Fast copy operation
Next Thread in C++ Forum Timeline: Array Minimum and Maximum





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


Follow us on Twitter


© 2011 DaniWeb® LLC