943,788 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 576
  • C++ RSS
Jul 29th, 2008
0

C to C++

Expand Post »
Hi, I would like to ask you for help in converting this code from C to C++. It's my little friend 's homework, but I can't help him 'cause I know nothing about C In fact, I just can't figure the "scanf" part out, I think the other part is similar to C++. Thanks in advance !
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3. void main()
  4. {
  5. int i,br=0,d=0;
  6. char r[200];
  7. printf("Vnesete Recenica\n");
  8. gets(r);
  9. d=strlen(r);
  10. for(i=0;i<=d;i++)
  11. {
  12. if((r[i]=='n')&&(r[i+1]=='o'))
  13. {
  14. r[i]=' ';
  15. r[i+1]=' ';
  16. br++;
  17. }
  18. }
  19. printf("Vo Vasata recenica se pronajdeni i izbrisani %d no zborovi\n",br);
  20. scanf("%d",&i);
  21. }
Last edited by Ancient Dragon; Jul 29th, 2008 at 9:13 am. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
robertmacedonia is offline Offline
85 posts
since Jul 2008
Jul 29th, 2008
1

Re: C to C++

First of all use code tags. Second do not use void main..main has to always return something. Then, what is the program supposed to do and what dont you understand about scanf..what kind of errors are you getting when you compile your code?
Reputation Points: 19
Solved Threads: 20
Posting Whiz in Training
joshmo is offline Offline
280 posts
since Oct 2007
Jul 29th, 2008
1

Re: C to C++

Hi, I would like to ask you for help in converting this code from C to C++. It's my little friend 's homework, but I can't help him 'cause I know nothing about C In fact, I just can't figure the "scanf" part out, I think the other part is similar to C++. Thanks in advance !
[
#include <stdio.h>
#include <string.h>
void main()
{
int i,br=0,d=0;
char r[200];
printf("Vnesete Recenica\n");
gets(r);
d=strlen(r);
for(i=0;i<=d;i++)
{
if((r[i]=='n')&&(r[i+1]=='o'))
{
r[i]=' ';
r[i+1]=' ';
br++;
}
}
printf("Vo Vasata recenica se pronajdeni i izbrisani %d no zborovi\n",br);
scanf("%d",&i);
}

]

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. void main()
  5. {
  6. int i,br=0,d=0;
  7. //char r[200];
  8. std::string r;
  9. //printf("Vnesete Recenica\n");
  10. std::cout<<"Vnesete Recenica\n"<<std::endl;
  11. //gets(r);
  12. std::cin>>r;
  13. //d=strlen(r);
  14. //for(i=0;i<=r.length();i++)
  15. /*
  16. for(i=0;i<r.length() - 1 ;i++) //THIS WAS INCORRECT IN THE 1st IMPLEMENTATION TOO, BUFFER OVERRUN !!
  17. {
  18. if((r.at(i)=='n')&&(r.at(i+1)=='o'))
  19. {
  20. r[i]=' ';
  21. r[i+1]=' ';
  22. br++;
  23. }
  24. }
  25. */
  26. std::string::size_type poz = -1;
  27. while ( true )
  28. if ( ( poz = r.find( "no", poz + 1 ) ) != std::string::npos )
  29. br++;
  30. else
  31. break;
  32.  
  33.  
  34. //printf("Vo Vasata recenica se pronajdeni i izbrisani %d no zborovi\n",br);
  35. std::cout<<"Vo Vasata recenica se pronajdeni i izbrisani "<<br<<" no zborovi\n"<<std::endl;
  36. //scanf("%d",&i);
  37. std::cin>>i;
  38. }

the last scanf/cin is placed there so that the program won't end untill the user sees the output and presses some key.
I placed the old C implementation between coments. It was also incorrect as it was causing buffer overuns on the char*
kux
Reputation Points: 66
Solved Threads: 11
Junior Poster
kux is offline Offline
119 posts
since Jan 2008
Jul 29th, 2008
1

Re: C to C++

Hi, I would like to ask you for help in converting this code from C to C++.
Code, that you posted is correct C++ code, except nonstandard main declaration.

But to be "just more C++", you may rewrite it as follows:

C++ Syntax (Toggle Plain Text)
  1. #include <string>
  2. #include <iostream>
  3.  
  4. int main(void)
  5. {
  6. std::string r;
  7. std::cout << "Vnesete Recenica" << std::endl;
  8. std::getline(std::cin, r);
  9. size_t d = r.size();
  10. int br = 0;
  11. for(int i = 0; i != d; ++i){
  12. if((r[i]=='n')&&(r[i+1]=='o')){
  13. r[i]=' ';
  14. r[i+1]=' ';
  15. br++;
  16. }
  17. }
  18. std::cout << "Vo Vasata recenica se pronajdeni i izbrisani " << br << " no zborovi" << std::endl;
  19. std::cin >> d;
  20. return 0;
  21. }

Also, we may use iterators and std::replace from <algorithm> header to make this code "really C++", but this simple task you may solve by yourself, I think.
Reputation Points: 46
Solved Threads: 2
Newbie Poster
Tilir is offline Offline
14 posts
since Jul 2008

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: writing to a file
Next Thread in C++ Forum Timeline: Files and Arrays





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


Follow us on Twitter


© 2011 DaniWeb® LLC