C to C++

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

Join Date: Jul 2008
Posts: 46
Reputation: robertmacedonia is an unknown quantity at this point 
Solved Threads: 0
robertmacedonia robertmacedonia is offline Offline
Light Poster

C to C++

 
0
  #1
Jul 29th, 2008
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 !
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 280
Reputation: joshmo is an unknown quantity at this point 
Solved Threads: 19
joshmo joshmo is offline Offline
Posting Whiz in Training

Re: C to C++

 
1
  #2
Jul 29th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: C to C++

 
1
  #3
Jul 29th, 2008
Originally Posted by robertmacedonia View 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 !
[
#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);
}

]

  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*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: Tilir is an unknown quantity at this point 
Solved Threads: 2
Tilir's Avatar
Tilir Tilir is offline Offline
Newbie Poster

Re: C to C++

 
1
  #4
Jul 29th, 2008
Originally Posted by robertmacedonia View Post
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:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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