0% found this document useful (0 votes)
4 views8 pages

Event-Controlled Looping in C++

This document contains code snippets demonstrating different types of looping and input/output in C++. The first snippet uses an event-controlled loop with a do-while statement to repeatedly print numbers from 0 to 5 until the user enters 'n' or 'N' when prompted. The second snippet uses a for loop to calculate and print the sum of integers from 1 to 5. The third snippet uses functions to print text to the console screen at given coordinates. The last snippet searches a text file for a user-input name and prints the corresponding boarding fee, handling errors if the file is not found.

Uploaded by

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

Event-Controlled Looping in C++

This document contains code snippets demonstrating different types of looping and input/output in C++. The first snippet uses an event-controlled loop with a do-while statement to repeatedly print numbers from 0 to 5 until the user enters 'n' or 'N' when prompted. The second snippet uses a for loop to calculate and print the sum of integers from 1 to 5. The third snippet uses functions to print text to the console screen at given coordinates. The last snippet searches a text file for a user-input name and prints the corresponding boarding fee, handling errors if the file is not found.

Uploaded by

sjabat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Event-Controlled Looping

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int main()
{ int x;
char ans;
do{
for(x=0;x<=5;x++)
{
cout<<x;
Sleep(1000);
}
cout<<\nTry again (y/n)?";
cin>>ans;
system("cls");

}while(ans=='y'|| ans=='Y');
system("pause>0");
}



Screen Output:

012345
Try again () y/n?
#include <iostream>
#include <windows.h>

using namespace std;
int main( )
{
int nR=5,nE,nI = 1, nC = 0;
for (nE = 1; nE <= nR; nE++)
{ nC += nI;
nI += 2;
}
cout<<nC;
system("pause>0");
}


Screen Output:

25

Event-controlled with String
Functions


#include <iostream>
#include <windows.h>
using namespace std;
void ERIC(int x, int y);
int main()
{
system("color C");
ERIC(40,10);cout<<"MAPUA";
system("pause>0");
}
void ERIC(int x, int y)
{
HANDLE eric;
COORD bogs;
bogs.X = x;
bogs.Y = y;
eric = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(eric,bogs);
}

MAPUA
Screen Output:

WANTED BOARDERS: ROOM 4 RENT
How many boarders? 2
Enter the Boarder's name: Elias
Enter the Boarder's name: Pinaglabanan

Nakopya na po sa database.



/* check for input failure in case the drive is
not accessible if true display a message
that the input file is not accessible return
the integer 1 from the main program */
if (!outName)
{
cout << "Di pupwede!\n";
return 1;
}
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <windows.h>
using namespace std;

int main()
{
ifstream inName;
string name, searchName;
double price;
bool found = false;
// open the input file
[Link]("[Link]");

if (!inName)
{
cout << "File not Found!\n";
system("pause");
return 1;
}


cout << setprecision(2) << fixed
<< showpoint;
cout << "Enter name to search: ";
getline(cin, searchName);
do {
/* read in the name on the file until SPACE is reached */
getline(inName, name, ' ');
if (name==searchName)
found = true;
inName >> price;
[Link]();
} while (!found && ![Link]());
if (found)
cout << "\nThe Boarding Fee for "
<< searchName << " costs " << price <<
endl;
else
cout << "\nThe Boarding Fee for " <<
searchName << " was not found\n";
[Link]();
system("pause>0"); }

Screen Output1:

File not Found!
Press any key to continue . . .



Note:

Screen Output1 will display if no
file is found on the drive path
Screen Output2:

Enter name to search: eric

The Boarding Fee for eric
costs 45.50

Note:

Screen Output2 will display if
file is found on the drive path

You might also like