Skip to main content

Useful C++ code snippets


Setting up  Precision to given decimal point 
“std:setprecision()”  method used along with  “std:fixed” provides precision of 
outputting floating point numbers correct to decimal numbers mentioned in the parameter 
of the setprecision method.
header file
#include <iomanip>
std:setprecision() defined no of digits in number. Without using std:fixed() method setprecision()  will   limit whole numbers digits to given precision.
In order to print precision of decimal point both setprecision() and fixed() method using before printing values,
code snippet :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// setprecision example

#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision

int main(){

     double   doubeleValue =3.34535 ;

     std::cout <<  std::setprecision(4) ;
     std::cout <<  doubeleValue<< '\n';
      // Output : 3.345  -without fixed


      std::cout << std::fixed;
      std::cout <<  doubeleValue<< '\n';// Output : 3.3453

     std::cout <<  std::setprecision(2) <<std::fixed<< doubeleValue<< '\n';
      // Output : 3.34

    return 0;
}  



Getting input from Terminal / CommandLine


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// setprecision example

#include <iostream>
#include <string>

using namespace std;

int main(){

    int getInt;
    std::cout << "Get Int : " ;

    cin >> getInt  ;
    std::cout << "int " << getInt<<'\n';
    // using cin >> you can get any primative data type

    /*  to get whole line as string use string and getline method
    */
    string str;
    std::cout << "String :" ;
    std::getline(std::cin,str);
    std::cout << str << '\n';

    return 0;
}


Getting multiple numeric values in same Line of Terminal/CMD
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <iterator>
#include <string>
#include <vector>

int main() 
{
    std::string s;

    std::getline( std::cin, s );

    std::istringstream is( s );

    std::vector<int> v( ( std::istream_iterator<int>( is ) ), std::istream_iterator<int>() );

    for ( int x : v) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

Getting the power of value
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
/* pow example */
#include <stdio.h>      /* printf */
#include <math.h>       /* pow */

int main ()
{
  printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) );
  printf ("4.73 ^ 12 = %f\n", pow (4.73, 12.0) );
  printf ("32.01 ^ 1.54 = %f\n", pow (32.01, 1.54) );
  return 0;
}

Reading a text file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// ios::exceptions
#include <iostream>
#include <fstream>
using namespace std;

void do_something_with(char ch) {} // Process the character 

int main () {
  ifstream file;
  file.exceptions ( ifstream::badbit ); // No need to check failbit
  try {
    file.open ("test.txt");
    char ch;
    while (file.get(ch)) do_something_with(ch);
    // for line-oriented input use file.getline(s)
  }
  catch (const ifstream::failure& e) {
    cout << "Exception opening/reading file";
  }

  file.close();

  return 0;
}

Writing a text file
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

Calculating the run time of given code
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}



 

Comments

Popular posts from this blog

How to proceed once you selected an organization in GSoC

How to proceed once you selected an organization in GSoC? In GSoC, each step you follow is very critical to get selected as a GSoC intern.For each step difficult increase and you have been specified what kind of specialize domain chose to select.Here is my experience in GSoC after I selected an organization. Once you selected an organization, you have done research to select a project which best suits your skills and time availability. First, find present and past project ideas given by the organization and note the projects you prefer. Then go through the mail achieves and fine mails about selected project ideas. From those mail archives, you can get a better understanding of those projects and what kind of future improvement they require. After that try to build an implementation for selected project and create a repository for that.then send a mail to selected organization mailing list stating your interest and showcase the what you have implemented. also, the...

Why The Internet is using two addressing schemes (IP and MAC) instead of a single addressing scheme?

Why The Internet is using  two addressing schemes (IP and MAC)  instead of a single addressing scheme?  The Internet is made up with different layers of the internet protocol suite.A Mac Address is the physical address of network interface assigned by manufacture. It isused to bring information to that computer on Layer 2 of the OSI model.Since MAC address scheme is flat addressing scheme If the internet is made up only using flat addressing scheme identification of a host would have been very inefficient. IP addressing scheme is hierarchical in nature, so identification of a host is much efficient. IP addresses are used on layer 3 and using the network portion of the IP address you will first identify which network the host is in and then with the host portion of the IP address you will find the exact host within that network. If internet have to use only IP address since IP address are signed manually to each device,it require higher infrastructural cost to implem...