Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Any suggestion to improve my coding skill?
New Posts  All Forums:Forum Nav:

[C++] Any suggestion to improve my coding skill?

post #1 of 10
Thread Starter 
This allows user to input string and swaps characters from front to rear. I thought about odd and even number of characters might mess up reverse or swap function. However, I didn't see any error message when i typed in odd number of characters and even number of characters. So, what do you think? please help me get better and leave any comment or suggestion!!! thank you!

Code:
//
// Description: 
//   User input that takes C string as an input parameter.
//   Reverse the string and output the result.
//
#include <iostream>
using namespace std;

void inputChar(char*);
//Prototype, request user input
//and save the number of character in global int.

void reverse(char*,const int);
//Prototype, determine the first and the last array with global int.
// const in global int. it can't be altered in reverse function.
//swap characters with swap function accordingly

void swap(char*, char*);
//Prototype, swap characters.

void printChar(const char*, const int);
//Prototype, print result after reverse and swap function.
// const in char and global int. it can't be altered in print function.

void enterToEnd();
//Prototype, enter to end cpp file.

int charSizeInArray=0;
//global int to store number of character user provided.

int main(){

  //Pointer point to dynamic character array with [256].
  char *charPtr = new char[256];

  /* input function */
  inputChar(charPtr);
  /* reverse swap function */
  reverse(charPtr, ::charSizeInArray);       //global variable.
  /* print result function */
  printChar(charPtr, ::charSizeInArray);     //global variable.
  /* enter to end function */
  enterToEnd();
  
  return 0;    
}

void inputChar(char* charResponse){
  cout << "Input : ";
  cin.getline(charResponse, 256);            //max at 256 char.
  /* do nothing but cout number of character */
  for( ; charResponse[::charSizeInArray]!='\\0';::charSizeInArray++);
}

void reverse(char* s, const int length){
  char *front=s,*rear = &s[length-1];
  /* 
     while loop will work on even and odd length;
     odd number will not swap because front < rear.
  */
  while(front < rear){                      
    swap(*front,*rear);
    front++;rear--;
  }
}

void swap(char* front, char* rear){
  char temp=*front;
  *front=*rear;
  *rear=temp;     
}

void printChar(const char* charResponse, const int sizeOfArray){
  cout << "The result is : ";
  for(int i=0; i<sizeOfArray; i++){
    cout << *charResponse++ << " ";
  }       
}

void enterToEnd(){
  cin.ignore(256, '\
');
  cout << "\
\
Press ENTER to continue..." << endl;
  cin.get();     
}

Edited by mkn1620 - 2/19/11 at 3:10pm
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #2 of 10
I'm not a c++ programmer.
But what do you mean by improving your code?

You could mean, neater syntax or being more efficient or writing fewer lines.
Serenity
(14 items)
 
Singularity
(13 items)
 
 
CPUMotherboardGraphicsRAM
[intel] i7 4790k [Asus] Maximus VII Impact [AMD] R9 Fury X [Crucial] Ballistix Sport VLP 16 GB Kit 
Hard DriveCoolingOSMonitor
[Samsung] SM951 M.2 256GB Custom Watercooling Windows 10 [Samsung] S27A950D 27" 120hz 
KeyboardPowerCaseMouse
[Filco] Ninja Majestouch-2 Athena Power AP-MFATX50P8 500W FlexATX Lian Li PC Q-12 [Mad Catz] R.A.T. 7 
Mouse Pad
[Razer] Goliathus 
  hide details  
Reply
Serenity
(14 items)
 
Singularity
(13 items)
 
 
CPUMotherboardGraphicsRAM
[intel] i7 4790k [Asus] Maximus VII Impact [AMD] R9 Fury X [Crucial] Ballistix Sport VLP 16 GB Kit 
Hard DriveCoolingOSMonitor
[Samsung] SM951 M.2 256GB Custom Watercooling Windows 10 [Samsung] S27A950D 27" 120hz 
KeyboardPowerCaseMouse
[Filco] Ninja Majestouch-2 Athena Power AP-MFATX50P8 500W FlexATX Lian Li PC Q-12 [Mad Catz] R.A.T. 7 
Mouse Pad
[Razer] Goliathus 
  hide details  
Reply
post #3 of 10
I think that for swapin a string you only have to swap half of the characters, or you'll end up with the same string.

Edit: Nvm didn't read the code properly.
Edited by _Chimera - 2/18/11 at 11:15pm
post #4 of 10
Code:
void inputChar(char*);
void reverse(char*,const int);
void swap(char*, char*);
void printChar(const char*, const int);
void enterToEnd();
int charSizeInArray=0; // put the function headers in a .h file
// separate your interface from implementation.  .h, .cpp files... 

int main(){  // should be int main(int argc, char *argv[]) {

  char *charPtr = new char[256]; // put that as a #DEFINE ARRAY_SIZE in your .h file.  the preprocessor is good for constants like that.

  inputChar(charPtr);
  reverse(charPtr, charSizeInArray);
  printChar(charPtr, ::charSizeInArray);
  enterToEnd();
  
  return 0;    
}
void inputChar(char* charResponse){
  cout << "Input : ";
  cin.getline(charResponse, 256); // again now if next week you want to jump to 512, that's two places you've got to dig for.  hope you don't miss one. 
  
  for(; charResponse[::charSizeInArray]!='\\0';::charSizeInArray++);  // i personally don't like for loops like that.  neat.  i get it.  a while loop is less taxing on the brain.. what is this line really doing anyway? seems odd..

}
void reverse(char* s, const int length){
  char *front=s,*rear = &s[length-1];
  while(front < rear){
    swap(*front,*rear);
    front++;rear--;  // neat. yay.  lots of ways you can do this, there's probably one that's even better.  always prefer preincrement to postincrement as post has to make a copy if it's an object.  only use post when it actually helps, i.e. preincrement throws you off.
  }
}

void swap(char* front, char* rear){
  char temp = *front; // why not join the declaration and assignment
  *front = *rear;
  *rear  = temp;    // put a space between arithmetic operators.  it's pretty much universally accepted for C style code.  exceptional c++ style is a decent book  
}

void printChar(const char* charResponse, const int sizeOfArray){
  cout << "The result is : ";
  for(int i=0; i<sizeOfArray; i++){
    cout << *charResponse++ << " "; // i think there's a trick where you can dump it out in one quick way and bag the loop.  i dont recall however.
  }       
}
void enterToEnd(){
  cin.ignore(256, '\
');
  cout << "\
\
Press ENTER to continue..." << endl;
  cin.get();     
}
not bad. i've seen a lot worse. i'm a bit fuzzy on c++ string tricks. But pointer arithmetic is probably what you're supposed to take away from this.
Edited by lloyd mcclendon - 2/18/11 at 11:36pm
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #5 of 10
Thread Starter 
I want to make it easier to read, easy to manage. I'm practicing Dynamic array, string, and pointer. Do I need to add delete [] charPtr at the end of main function to free memories? I think once the main ends, the memories will be released. but, is it a good practice to include "delete [] charPtr" at the end of main?

Thx guys, this really helps

Quote:
Originally Posted by dcyli View Post
I'm not a c++ programmer.
But what do you mean by improving your code?

You could mean, neater syntax or being more efficient or writing fewer lines.
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #6 of 10
You should put some comments in your code so that if you ever come back to it at a later time or give it to someone else to improve / modify it they will know whats going on.
post #7 of 10
Quote:
Originally Posted by melterx12 View Post
You should put some comments in your code so that if you ever come back to it at a later time or give it to someone else to improve / modify it they will know whats going on.
Sorry dont have time to look at your code now but this is a very important thing to do. Have saved me looooooots of headache.
The Kandalf
(16 items)
 
  
CPUMotherboardGraphicsGraphics
I7 5820K MSI X99S Gaming 7 ASUS R9 280X TOP Crossfire X ASUS R9 280X TOP Crossfire X 
RAMHard DriveOptical DriveOS
Crucial DDR4 2133MHz 8GB (2x4GB) Samsung SpinPoint F1 1TB HP DVD630 Ubuntu 
MonitorKeyboardPowerCase
2x Philips Brilliance 220CW Microsoft Wireless Desktop Elite Keyboard Fractal Design Newton R3, 800W 80+ Platinum Corsair 900D 
MouseMouse PadAudioAudio
Mionix Naos 8200 Razer Pro Solutions Arcam rDAC B&W CM1 
  hide details  
Reply
The Kandalf
(16 items)
 
  
CPUMotherboardGraphicsGraphics
I7 5820K MSI X99S Gaming 7 ASUS R9 280X TOP Crossfire X ASUS R9 280X TOP Crossfire X 
RAMHard DriveOptical DriveOS
Crucial DDR4 2133MHz 8GB (2x4GB) Samsung SpinPoint F1 1TB HP DVD630 Ubuntu 
MonitorKeyboardPowerCase
2x Philips Brilliance 220CW Microsoft Wireless Desktop Elite Keyboard Fractal Design Newton R3, 800W 80+ Platinum Corsair 900D 
MouseMouse PadAudioAudio
Mionix Naos 8200 Razer Pro Solutions Arcam rDAC B&W CM1 
  hide details  
Reply
post #8 of 10
Thread Starter 
thx Iloyd,
Several question, i setup char[256],
Code:
char *charPtr = new char[256];
can i use char[size] and base on the user's input, it will determine the size of array for each character the user input. If i can, How do you determine the length? use <string> and .length()?
Code:
int numberOfCharacter = charResponse.length()
The reason I created this
Code:
for(; charResponse[::charSizeInArray]!='\\0';::charSizeInArray++);
is because it returns global charSizeInArray to swap characters.
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #9 of 10
Thread Starter 
Quote:
Originally Posted by melterx12 View Post
You should put some comments in your code so that if you ever come back to it at a later time or give it to someone else to improve / modify it they will know whats going on.
Yeah i totally forgot about it. thx, ill keep that in mind next time.
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
post #10 of 10
Thread Starter 
i added comments in my code now. it should be very easy to read now.
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
Rampage 2 Extreme
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 D0 @ 3.8G Rampage II Extreme EVGA GTX295 Corsair 6G 15000 
Hard DriveOSPowerCase
500G Window Pro x64 / Ubuntu 10.04 Corsair 1000HX Rave01 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Any suggestion to improve my coding skill?