0% found this document useful (0 votes)
1K views6 pages

General C++ Cheat Sheet

The document provides an overview of C++ concepts including variables, data types, functions, conditionals, loops, arrays, and more. It includes syntax examples and explanations of concepts like calling functions, input/output, math functions, and random numbers.

Uploaded by

Meower
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views6 pages

General C++ Cheat Sheet

The document provides an overview of C++ concepts including variables, data types, functions, conditionals, loops, arrays, and more. It includes syntax examples and explanations of concepts like calling functions, input/output, math functions, and random numbers.

Uploaded by

Meower
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C++

Review
MAIN INFO
Calling for Functions & Namespace
#include
#include
#include
#include
#include

<iostream>
<string>
<cmath>
<cstdlib>
<time.h>

<<<<<-

Input & Outputs


Calling for Variable String
Math Functions
Random Number
Random Number Addon (Time)

using namespace std;

---------------------------------------------------------------------

Variables

else{
return 0;}
}
int main()
{
cout<<"From (x= 499.98, y= 499.97, z= 499.99) The Maximum is:
"<<maximum(499.98, 499.97, 499.99)<<endl;

int soccerNum = 9;

return 0;
}
---------------------------------------------------------------------

double decimals = 9.4502;

Calling a Function in Main

char apple='a';

string myName = "Juan Pablo Garza;

---------------------------------------------------------------------

Cout
cout<<Hello World!<<endl;

---------------------------------------------------------------------

Cin
int Num1;
cout<<Enter A Number: ;
cin>> Num1

---------------------------------------------------------------------

Else

void converttime(int seconds, int& hours, int& min, int& sec)


{
sec = seconds % 60;
min = seconds % 3600;
min = min / 60;
hours = seconds / 3600;
}
int main()
{
int seconds,h,m,s;

if (TRUE){
// Execute these statements if TRUE
}

cout<<"Type in your seconds: ";


cin>>seconds;

else {
// execute these statements if FALSE
}

converttime(seconds,h,m,s);

---------------------------------------------------------------------

Else If
if (<condition>){
// Execute these statements if TRUE
}
else if (<another condition>){
// execute these statements if <another condition> is TRUE
// and <condition> is FALSE
}

---------------------------------------------------------------------

Switch
switch (code) { //add as many cases.
case 1:
cout<<"3M Corporation"<<endl;
break;
case 2:
cout<<"Maxell Corporation"<<endl;
break;
default:
break;
}

---------------------------------------------------------------------

While Loop
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double amount=1000,interest=.0325;
int count = 1;

cout<<"From "<<seconds<<" seconds we get "<<h<<" hours, <<m<<"


minutes, and "<<s<<" seconds."<<endl;
return 0;
}
---------------------------------------------------------------------

Random Numbers
int i=0, j=0, k=0;
int main()
{
for (i=0; i<10;i++) {
int v1,v2,answer;

v1 = rand()% 10;
v2 = rand()% 100 + 1;
v3 = rand()% 30 + 1985;

// v1 in the range 0 to 9
// v2 in the range 1 to 100
// v3 in the range 1985-2014

cout<<"How much is "<<v1<<" times "<<v2<<"? "<<endl;


cin>>answer;
if (v1*v2==answer) {
cout<<"CORRECT!\n";
k++;
}

while (count<=10) {
amount= amount +(amount*interest);
cout<<"Amount at the end of the year"<<count<<"is ($): "<<amount<<endl;
count++;
}

else{
cout<<"INCORRECT!\n<<";
j++;}

---------------------------------------------------------------------

For Loop
#include <iostream>
using namespace std;
int main()
{
double balance=1000.0;
(initialization,limit,count)
for (int i=0;i<10;i+=1){
//balance = balance + (balance*.08)
//balance = balance*1.08
balance *= 1.08;
cout<<"For year "<<i+1<<" your balance is $ <<balance<<endl;
}
return 0;
}

---------------------------------------------------------------------

Functions
double square(double a)
{
return a*a;
}
int main()
{
cout<< "9 squared is " << square(9) << endl;
return 0;
}
double maximum (double x, double y, double z)
{

double percentage=k*10;
if (percentage<80) {
cout<<"You got "<<percentage<<"% correct, Please ask your instructor
for extra help."<<endl;
}
else { cout<<"Good Job! you got "<<percentage<<"% Correct!"<<endl;
return 0;
}
}
---------------------------------------------------------------------

MATH
Add
> +
Remainder > %
Or
> ||
Subtract
> x^2
> pow(x,2)
And > &&
Divide
> /
e^2
> exp(2)
Multiply
> *
Equality
> ==
---------------------------------------------------------------------

EXAMPLES
int main() {
int month,day;
string season;
cout<<"Enter numerical Month: "<<endl;
cin>>month;

if (x>y && x>z) {


return x;
}

cout<<"Enter Date: "<<endl;


cin>>day;

if (y>x && y>z) {


return y;
}

if (month == 1 || month == 2 || month == 3)


season = "Winter";

if (z>x && z>y) {


return z;
}

else if (month == 4 || month == 5 || month == 6)


season = "Spring";

C++ Review
else if (month == 7 || month == 8 || month == 9)
season = "Summer";
else
season = "Fall";

#include <time.h>
using namespace std;
int main() {
double i, p=0,tot=0,avg1,num;

------- 2nd Part


if (month % 3 == 0 && day >= 22)
{
if (season == "Winter")
season = "Spring";

cout<<"Type the total number of data values to be averaged: ";


cin>>i;
while (p<i) {
cout<<"Enter Number: ";
cin>>num;

else if (season == "Spring")


season = "Summer";

cout<<"Your Number in entry "<<p + 1<<" is "<<num<<endl;


tot=tot+num;

else if (season == "Summer")


season = "Fall";

p++;
}
avg1=tot/i;
cout<<"Your Average is = "<<avg1<<endl;

else
season = "Winter";
cout<< "Season: " << season << endl;
}
return 0; }
---------------Display Triangle------------
int main()
{
int r=5;
// r = the last row
char star='*';
char space=' ';
for (int i=1; i<=r; i++) // i = the row where that I want to show
first {

return 0; }
//-------------------------REVIEW QUESTION 2------------------------#include <iostream>
#include <cmath>
using namespace std;
double mult(double x, double y)
{
return x*y;
}

for (int s=1; s<=r-i; s++) // s = how many spaces per row
cout<<space;

int main() {

for (int a=1; a<=2*i-1; a++) // a = how many asterixs per row
cout<<star;
cout<<endl; }
------ Display Downward Triangle ---

return 0; }
//-------------------------REVIEW QUESTION 3------------------------#include <iostream>

for (int i=4; i>0; i--)


first
{

// i = the row where that I want to show

for (int s=1; s<=r-i; s++)


cout<<space;

// s = how many spaces per row

for (int a=1; a<=2*i-1; a++) // a = how many asterixs per row
cout<<star;
cout<<endl; }
return 0;
}
-------- Pythagoras Theorem -------
int main()
{
for (int a = 1; a <= 500; a++)
{
for (int b = 1; b <= 500; b ++)
{
for (int c = 1; c <= 500; c ++)
{
if ((a*a + b*b) == (c*c))
cout<<"a= "<<a<<", b= "<<b<<", c= "<<c<<endl;
}
}
}
return 0;
}
----- Function to find dates after.. ---
void yrtime(int& days,int& year, int& month, int& day);

cout<<"2*2 = "<< mult(2,2)<<endl;

using namespace std;


double maximum(double x, double y, double z) {
double max=x;
if (y>max) {
max=y;
}
if (z>max) {
max=z;
}
}

int main()
{
cout<<"The maximum in x=3, y=8, z=10 is: "<<maximum(3, 8,
10)<<endl;
return 0; }
//-------------------------REVIEW QUESTION 4------------------------#include <iostream>
using namespace std;
void max_and_avg(double& first,double& second, double& third, double&
max, double& avg)
{
max=first;
if (second>max) {
max=second; }

int main() {
int days,y,m,d;

if (third>max) {
max=thir }

cout<<"Enter Days: ";


cin>>days;
yrtime(days,y,m,d);
cout<<days<<" days since the turn of the last century (1/1/1900)
makes the day to be ("<<d+1<<"/"<<m+1<<"/"<<y+1990<<")"<<endl;
return 0;}
void yrtime(int& days,int& year, int& month, int& day)
{
year=days/365;
month=days%365;
month=month/30;
day=days%365;
day=day%30;
}
//-------------------------REVIEW QUESTION 1------------------------#include <iostream>
#include <string>
#include <cmath>
#include <math.h>
#include <cstdlib>

return max;

avg=(first+second+third)/3;

int main() {
double x, y, z, max, avg;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
cout<<"Enter third number: ";
cin>>z;
max_and_avg(x, y, z, max, avg);
cout<<"The Highest number you entered is: "<<max<<endl;
max_and_avg(x, y, z, max, avg);
cout<<"And the Average is: "<<avg<<endl;
return 0;}

C++ Review
---------------------------Grades and average------------------------

MAIN INFO
Calling for Functions & Namespace

int main()
{
int const ROW = 3, COL = 2;

---------------------------------------------------------------------

int total[ROW], grade[ROW][COL];

Arrays

for (int i = 0; i < ROW; i++)


{
total[i] = 0;
for (int j = 0; j < COL; j++)
{
cout << " Enter a grade: " << endl;
cin >> grade[i][j];
total[i] = total[i] + grade[i][j];
}
}

int number[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
number[0] number[1] number[2] number[11]

Number
:
&Number
:
&number[0] :

1245020
1245020
1245020

int *pNumber = number


number[0]
*pNumber

:
:

31
31

Number+1
:
&Number[1] :

1245024
1245024

// first initialize the pointer

for (int i = 0; i < ROW; i++)


{
for (int j = 0; j < COL; j++)
{
cout << grade[i][j] << ", ";
}
cout << " Average = " << total[i] / COL << endl;
}

//you can use max=INT_MIN to get the minimum value of the array

--------------------------------------------------------------------int main()

// This example shows the a column of 9 arrays in first for loop,


and then a matrix of 3 by 3 from the 2nd for loop.
{
const int VALUES = 9;
double prices[VALUES] = {10.95, 16.32, 12.15, 6.22, 15.98, 26.43, 17.54, 6.45,
17.59};
int i;
for(i = 0; i < VALUES; i++)
cout << prices[i] << endl;

system("pause");
return 0;
}

----------------------------Max value per Row------------------------

for(i = 0; i < VALUES; i = i + 3)


cout << prices[i] << "\t" << prices[i+1] << "\t" << prices[i+2] << endl;
// "\t" is like clicking on the tab key.

int main()
{
const int ROWS=10;
const int COLS=20;
int fmax[ROWS][COLS];
for(int i=0; i<ROWS;i++)
{
for(int j=0; j<COLS;j++)
{
fmax[i][j]=(rand() % 101);
}
}

system("pause");
return 0;
}

--------------------------------------------------------------------#include <iostream>
using namespace std;
int main()
{
int const GRADES = 15;

for(int i=0; i<ROWS; i++)


{
for(int j=0; j<COLS; j++)
{
cout << fmax[i][j] << "\t";
}
cout << endl;
}

int grades[GRADES] = {89,95,72,83,99,54,86,75,92,73,79,75,82,73,87},


variance,
deviation[GRADES],
average,
total = 0;
for (int i = 0; i < GRADES; i++)
{
total = total + grades[i];
}

int max=0,loci=0,locj=0;
for(int i=0; i<ROWS;i++)
{
max=0;
for(int j=0; j<COLS;j++)
{
if (max<fmax[i][j])
{
max=fmax[i][j];
loci=i;
locj=j;
}
}

average = total / GRADES;


cout << "(Deviation, Grade)" << endl;
// DEVIATION is obtained as the element value less the average of all the data.
for (int i = 0; i < GRADES; i++)
{
deviation[i] = grades[i] - average;
cout << "(" << deviation[i] << ", " << grades[i] << ")" << endl;
}
// The VARIANCE is obtained by squaring each individual deviation and dividing the
sum of the squared deviations by the number of deviations.
total = 0;
for (int i = 0; i < GRADES; i++)
{
total = total + (deviation[i] * deviation[i]);
}
variance = total / GRADES;

cout<<"\nThe maximum value of row "<<i<<" is: "<<max<<"."<<endl;


cout<<"This is the element with reference "<<loci+1<<", "<<locj+1<<" in the
list of numbers"<<"."<<endl;
}
return 0;
}

--------------------------Arrays and Pointers-----------------------int main()


{
double *pt;
double price[]={12.99,18.69,11.44,13.75,9.59,15.29,17.69};

cout << "Variance = " << variance << endl;

pt = price;

system("pause");
return 0;

int i;
for(i = 0; i < 7; i++)
cout << *(price + i) << endl;

------------------Alternating Sums (w/ Random Numbers)--------------#include<iostream>


#include<cmath>
#include<cstdlib>
using namespace std;

system("pause");
return 0;
}
#include <iostream>
using namespace std;

int main(){
int alternating_sum[10], s=0;

void zeropadshift(int[], int, int);


void display(int[], int);

for(int i=0; i<10; i++){

//display array

----------------------------Arrays and Shifts------------------------

alternating_sum[i] = rand() % 20;

int main()
{
int a[7] = {1,2,3,4,5,6,7};

cout<<alternating_sum[i]<< ' ';


}

display (a,7);
zeropadshift(a, 7, 2 );
display (a,7);

cout<<endl;
for(int i=0; i<10; i++){

//initialize array
//display before shifting
//shifting
//display after shifting

return 0;
}

s+= pow(-1,i) * alternating_sum[i];

//"s+" makes the first value positive

}
cout<<s<<endl;
return 0;
}

CONTINUED------------------------Next Page---------->

C++ Review
void zeropadshift(int array[], int size, int distance)
{
int temp=0;
if (distance > 0 && distance <= size)
{
for (int i = size - 1; i > distance - 1; i--)
{
array[i] = array[size - distance - temp - 1];
temp++;
}

------------------------Change the es in string--------------------int main()


{
string a;
cout << " Enter a string: " << endl;
getline (cin, a);
for(int i = 0; i < a.length(); i++)
{
if(a[i] == 'e')
a[i] = 'x';
}
cout << a << endl;

for (int i = 0; i < distance; i++)


array[i] = 0;

//array length

system("pause");
return 0;

}
}

//same as "cin>> a;"

-----------------------------Counts the spaces----------------------void display(int array[], int size)


{
cout<<"The array is: ";
for (int i = 0; i < size; i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
}

int main()
{
string a;
cout << " Enter a string: " << endl;
getline (cin, a);
int count = 0;
for(int i = 0; i < a.length(); i++)
{
if(a[i] == ' ' && a[i + 1] != ' ')
count++;
}

------------------------Display Time differently--------------------#include <iostream>


#include <string>

if(a[a.length() - 1] == ' ')


cout << count << endl;
else
cout << count +1 << endl;

using namespace std;


struct time{
int hour;
int minutes;

return 0;
}

};

struct stock
{
char name[10];
double pshare, ratio, pvalues;
};

int main()
{
struct time militaryTime;
int hour;
string cycle;

---------------------------------Stocks-----------------------------

cout<<"Input the hour in military form (0-23):";


cin>>militaryTime.hour;

int main()
{
stock s[5];

cout<<"Input the minutes (0-59):";


cin>>militaryTime.minutes;

for(int i = 0; i < 5; i++)


{
cout << "Enter the name of the stock " << i+1 << ": ";
cin >> s[i].name;
cout << "Enter the estimated earnings per share of stock " << i+1 << ": ";
cin >> s[i].pshare;
cout << "Enter the estimated price-to-earnings ratio " << i+1 << ": ";
cin >> s[i].ratio;
}

if (militaryTime.hour>=12){
hour=militaryTime.hour-12;
cycle= "pm";
}
else { cycle= "am"; }

for(int i = 0; i < 5; i++)


{
cout << s[i].name;
s[i].pvalues = s[i].pshare * s[i].ratio;
cout << s[i].pvalues << endl;
}

cout<<"for hour= "<<militaryTime.hour<<", minute= "<<militaryTime.minutes<<", the


display is "<<hour<<":"<<militaryTime.minutes<<cycle<<endl;
return 0;
}

--------------------Giving Array values with pointers---------------int main()


{
double pt;

--------------------------File to another File-----------------------

int intValues[]={};
pt = *intValues;
for (int i=0; i < 5; i++)
{
cout<<"Enter desired value for array " <<i+1<<": ";
cin>>*(intValues+i);
}
cout<<endl;
for (int i=0; i < 5; i++)
{
cout<<"intValue["<<i<<"] contains the value --> "<<intValues[i]<<endl;
}
return 0;
}

--------------------Changing string Backwards-----------------------void reverse(char s[])


{
int i = 0, count = 0;
while(s[i] != '\0'){
in the for loop
i++;
count ++;
}
char *pt;
pt = s;

//These loop gives me the value for the COUNT that im using

for(int i = 0; i < count; i++)


{cout<<*(pt + count - i - 1);
}
}
int main()
{
char s[100];
cout << " Enter a Name: ";
cin >> s;
reverse(s);
cout << endl;
system("pause");
return 0;
}

system("pause");
return 0;
}

// This way it gets to array[0], we need that '- 1'

#include
#include
#include
#include

<iostream>
<fstream>
<cstdlib>
<string>

using namespace std;


int main()
{
string text;
ifstream file;
ofstream second;
file.open("employee.txt");
second.open("employee.bak");
while(file.good()){
getline(file,text);
second<<text;
}
second.close();
file.close();
return 0;
}

--------------------------------Average MPG-------------------------struct car


{
int num, miles, gallons;
double mpg;
};
int main()
{
car s[5];
for(int i = 0; i < 5; i++)
{
cout << "Enter the car number: ";
cin >> s[i].num;
cout << "Enter the miles driven: ";
cin >> s[i].miles;
cout << "Enter the gallons used: ";
cin >> s[i].gallons;
}
double total = 0;
for(int i = 0; i < 5; i++)
{
cout << "Car number: " << s[i].num;
s[i].mpg = s[i].miles / s[i].gallons;
cout << " MPG: " << s[i].mpg << endl;

total = total + s[i].mpg;


}
cout << "Average MPG total is: " <<
total / 5 << endl;
system("pause");
return 0;
}

C++ Review
}

------------------------------Who is Older?-------------------------#include <iostream>


#include <string>

cout << '\n';


system("pause");

using namespace std;


return 0;
}
void days(int x, int y, int z)
{
cout << "Please enter the current month: " << endl;
cin >> x;
cout << "Please enter the current (day) date: " << endl;
cin >> y;
cout << "Please enter the current year " << endl;
cin >> z;
}
void older(int x, int y)
{
if(x<y)
{
cout<<"Person A is older"<<endl;
}
else
{
cout<<"Person B is older"<<endl;
}
}
int main()
{
struct Date { int month; int day; int year; };
//all years have 360 days and each month consists of 30 days
Date start[1];
cout << "Please enter Person A's birthday month; " << endl;
cin >> start[0].month;
cout << "Please enter Person A's birthday day; " << endl;
cin >> start[0].day;
cout << "Please enter Person A's birthday year; " << endl;
cin >> start[0].year;
Date days[1];
cout << "\nPlease enter Person B's birthday month; " << endl;
cin >> days[0].month;
cout << "Please enter Person B's birthday day; " << endl;
cin >> days[0].day;
cout << "Please enter Person B's birthday year; " << endl;
cin >> days[0].year;
int adays=0, bdays=0;
adays = ((((start[0].month)* 30) - 30) + (start[0].day)+(((start[0].year)* 360) 360));
bdays = ((((days[0].month)* 30) - 30) + (days[0].day)+(((days[0].year)* 360) 360));

-----------------------------Total Gross Pay------------------------#include <string>


#include <iostream>
using namespace std;
int NUM=6;
struct Employee{
int Number;
string Name;
float Rate;
float Hours;
float Grosspay;
};
void payroll(Employee *a){
float total=0;
for (int i = 0; i < NUM; i++){
a[i].Grosspay = a[i].Rate*a[i].Hours;
cout << a[i].Name << "\t " << a[i].Number << "\t " << a[i].Grosspay <<
endl;
total+= a[i].Grosspay;
}
cout<<"The total gross pay of all employees is: "<<total<<endl;
}

float highest(Employee *a){


float max = a[0].Grosspay;

older(adays, bdays);

for (int i = 0; i < NUM; i++){


if (max < a[i].Grosspay)

cout << '\n';

max = a[i].Grosspay;

//system("pause");
cin.get();

return 0;

return max;

#include <iostream>
#include <string>

int main()

using namespace std;

{
Employee employee[6];//declare

void days(int x, int y, int z)


{
cout << "Please enter the current month: " << endl;
cin >> x;
cout << "Please enter the current (day) date: " << endl;
cin >> y;
cout << "Please enter the current year " << endl;
cin >> z;
}

employee[0].Number = 3462;

---------------------How many days have passed?----------------------

employee[1].Name = "Robbins";

int main()
{
struct Date { int month; int day; int year; };
//all years have 360 days and each month consists of 30 days

employee[1].Rate = 5.83;

employee[0].Name = "Jones";
employee[0].Rate = 4.62;
employee[0].Hours = 40;
employee[1].Number = 6793;

employee[1].Hours = 38;
employee[2].Number = 6985;

Date start[1];
cout << "Please enter a starting month; " << endl;
cin >> start[0].month;
cout << "Please enter a starting day; " << endl;
cin >> start[0].day;
cout << "Please enter a starting year; " << endl;
cin >> start[0].year;

employee[2].Name = "Smith";
employee[2].Rate = 5.22;
employee[2].Hours = 45;
employee[3].Number = 7834;

Date days[6];
employee[3].Name = "Swain";
int since[5];
employee[3].Rate = 6.89;
for (int i = 0; i < 5; i++)
{
cout << "Please enter a range-to month; " << endl;
cin >> days[i].month;
cout << "Please enter a range-to day; " << endl;
cin >> days[i].day;
cout << "Please enter a range-to year; " << endl;
cin >> days[i].year;
}

employee[3].Hours = 40;
employee[4].Number = 8867;
employee[4].Name = "Timmins";
employee[4].Rate = 6.43;
employee[4].Hours = 35;

for (int i = 0; i < 5; i++)


{
since[i] = ((((days[i].month)* 30) - 30) + (days[i].day)+(((days[i].year)* 360) 360)) - (((1 * 30) - 30) + (1 * 1) + ((1900 * 360) - 360));
cout << "\n Since the date " << start[0].month << "." << start[0].day <<
"." << start[0].year << " to the date " << days[i].month << "." << days[i].day <<
"." << days[i].year << ", " << since[i] << " days have passed." << endl;

employee[5].Number = 9002;
employee[5].Name = "Williams";
employee[5].Rate = 4.75;

C++ Review
employee[5].Hours = 42;

return 0;}

payroll(employee); //the name of the array is the first address of the


element; means employee
//is the pointor points to employee[0]
-----------------------------------2nd Part---------------------------------------float maxpay = highest(employee);//the name of the array is the first address
of the element;
cout << "The highest gross pay is "<<maxpay << endl;
cin.get();
return 0;
}
-----------------------------------More on Pointers-------------------------------#include <iostream>
using namespace std;
int main()
{
double miles[10] = {240.5,300,189.6,310.6,280.7,206.9,199.4,160.3,177.4,192.3},
gallons[10] = {10.3,15.6,8.7,14,16.3,15.7,14.9,10.7,8.3,8.4},
mpg[10];
double *m, *g, *mp;
m = miles, g = gallons, mp = mpg;
for(int i = 0; i < 10; i++)
{
*(mp + i) = *(m + i) / *(g + i);
cout << *(mp + i) << endl;
}
system("pause");
return 0;
}
-------------------------------Random Cols and Rows-------------------------------int main()
{
int const ROW = 4, COL = 4;
int i, j, grades[ROW][COL];
for (i = 0; i < ROW; i++)
{
for (j = 0; j < 3; j++)
{
// srand(time(0));
grades[i][j] = (rand() % 61 + 40); //From "40" to 61+40=101 so "100"
}
grades[i][3]= grades[i][0]*.5 + grades[i][1]*.2

+ grades[i][2]*.3;

}
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
cout << grades[i][j]<<"\t";
}
cout << endl;
}
system("pause");
return 0;
}
-----------------------Skips leading Whitespaces in Each Line---------------------#include "stdafx.h"
#include "string"
#include "stdlib.h"
#include "stdio.h"
#include "sstream"
#include "iostream"
#include "fstream"
using namespace std;
string& trim(string &str, string::size_type pos = 0)
{
static const string delim = " \t";
pos = str.find_first_of(delim, pos);
if (pos == string::npos)
return str;
return trim(str.erase(pos, 1));
}
void Test(void)
{
char *str1 = NULL;
str1 = new char[20];
char str[7];
strcpy_s(str1, 20, "hello world");
strcpy_s(str, "hello");
cout << "strlen(str1):" << strlen(str1) << endl << "strlen(str):" <<
strlen(str) << endl;
printf(str1);
printf("\n");
cout << str << endl;
system("pause");
}
int main(int argc, char* argv)
{
string line;
//char* deleted_file;
ifstream infile("verse.txt");
ofstream outfile("modified_verse.txt");
while (getline(infile, line)){
//deleted_file = const_cast<char*>(line.c_str());
trim(line);
outfile<<line;
}
//Test();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy