Problems with C + + training (1)

Source: Internet
Author: User

C + + Training Human Resource management system

This is a very small system, not even the system, but with some basic functions for practiced hand.

https://pan.baidu.com/disk/home? #list/path=%2f&vmode=list This is the address of the cloud disk, which has the documentation for this training project (very simple).

content :

Based on this small project, this blog post will list the problems I encountered while writing this system and how to solve them.

(PS: my C + + just learned fur, not very deep, just a novice, if you find errors in the process of browsing, you can leave a message, I will promptly correct, thank you very much )

Body:

1: Open TXT file, read and write Data

#include <iostream>#include<stdio.h>#include<string>#include<fstream>using namespacestd;intMain () {ifstream dire ("Pmtxt.txt"); //Open File Pm.txt, and name it dire, read file contents, read operation (input) file classDire.fail () = =1;//determine if the file is open failed 1: Open failed; 0: Open SuccessOfstream Tofil ("Pm.txt");//Open File Pm.txt, and name it Tofil, write operation (output) of the file classFStream Records ("Pm.txt");//This also allows you to open a readable    return 0; }

2: Read and write file contents

Read file line contents:getline ()

#include <iostream>#include<stdio.h>#include<string>#include<fstream>using namespacestd;intMain () {stringLine ; Ifstream Dire ("Pmtxt.txt"); //Open File Pm.txt, and name it dire, read file contents, read operation (input) file classDire.fail () = =1;//determine if the file is open failed 1: Open failed; 0: Open SuccessOfstream Tofil ("Pm.txt");//Open File Pm.txt, and name it Tofil, write operation (output) of the file classFStream Records ("Pm.txt");//This also allows you to open a readablegetline (Dire,line); //read a line in the dire file and save it in linescout<<line;//Display this line of contenttofil<<line;//writes the line string to the Tofil filetofil<<"a newly written line"<<Line ; return 0; }

3: Close File

Dire.close ();

Tofil.close ();

4: formatted output

Header file #include <iomanip>

Fixed length: setw (int w)

COUT<<SETW (8) << "line" <<endl; Fixed length of 8 bits

Left justified/right justified
cout<<std::right<<setw (Ten) << "123"; Right-aligned, fixed-length 10-bit

COUT<<SETW (5) << "123" <<endl;//right align
cout<<std::left<<setw (Ten) << "123";//left-aligned, fixed-length 10-bit

COUT<<SETW (6) << "123" <<endl;//left Justified
Once left-justified, or has right-aligned, if it is not changed around it, it will remain previously set

5: Case Conversion

#include <iostream>#include<stdio.h>#include<string>#include<algorithm>using namespacestd;intMain () {stringSTR1,STR2,STR3; CIN>>str1; Transform (Str1.begin (), Str1.end (), Back_inserter (STR2),:: ToLower);//Full lowercase conversiontransform (Str1.begin (), Str1.end (), Back_inserter (STR3),:: ToUpper); //full capitalization conversion to empty when STR2,STR3 is to be reused    return 0; }

6: character array converted to string

#include <iostream>#include<stdio.h>#include<string>using namespacestd;intMain () {Charstr1[ -]="aaaaaa"; stringSTR2=STR1;//entire full conversion to string    stringSTR3 (&str1[0],&str1[5]);//(start position, end position) to stringcout<<str2<<Endl; cout<<str3<<Endl; return 0; }

7:Some methods of the string class
str.substr(starting position, number of characters (not written to last))

Str.find(Match string, starting position, first few matching string)

str.rfind(Match string, String::npos: Indicates a bit from the target string, first several matching strings) returns the position of the first character, or 1str.replace(start position, delete number, insert content) returns the replaced contentThe original string is modified 
#include <iostream>#include<stdio.h>#include<string>using namespacestd;intMain () {stringSTR1,STR2,STR3; intn1,n2; CIN>>str1; CIN>>STR3; N1=str1.find ('C'); STR2=str1.replace (N1,1,1,'o');//(start position, delete number, character ' C ' repetition, ' character ');cout<<str1<<Endl; cout<<str2<<Endl; N2=str3.find ("AB"); STR2=str3.replace (N2,1,"ooo");//(start position, delete number, insert string);cout<<str2<<Endl; cout<<str3<<Endl; return 0; }

8: How to receive the carriage return and Judge  
#include <iostream>#include<stdio.h>#include<string>using namespacestd;intMain () {Charstr[ -]; intI=0, flag=1, N; GetChar ();//If you have a carriage return before, it can swallow the carriage return     while(flag) {Str[i]=getchar ();//Get input characters//or: scanf ("%c", &str[i]);        if(Str[i] = ='\ n') {cout<<"Receive carriage return"<<Endl; Flag=0; I++; }//receive Enter to exit    }    stringSTR1 (&str[0],&str[i-1]);//converts a character array to a string that does not contain a carriage returncout<<str1<<Endl; return 0; }

For the time being, write here.

Problems with C + + training (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.