why can't i convert please help

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

Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

why can't i convert please help

 
0
  #1
Jun 29th, 2009
its in the num=="-".


  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: why can't i convert please help

 
0
  #2
Jun 29th, 2009
this is code that won't convert.

int b=atoi(cin.get(num));
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 441
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: why can't i convert please help

 
1
  #3
Jun 29th, 2009
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: why can't i convert please help

 
0
  #4
Jun 29th, 2009
k thanks.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: why can't i convert please help

 
0
  #5
Jun 29th, 2009
still confused why it won't convert.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: why can't i convert please help

 
0
  #6
Jun 29th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 441
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: why can't i convert please help

 
0
  #7
Jun 29th, 2009
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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: why can't i convert please help

 
0
  #8
Jun 29th, 2009
i am supposed to this without pointers. The next topic is pointers.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 441
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: why can't i convert please help

 
0
  #9
Jun 29th, 2009
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?
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: why can't i convert please help

 
0
  #10
Jun 29th, 2009
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.
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