include <iostream>
#include<stdio.h> 
#include<stdlib.h> 
#include<conio.h> 
#include<iostream.h> 
#include<fstream.h> 
#include<string.h> 
#include<iomanip.h>
class student
{
public: char name[10];
char usn[10]; char age[5]; char sem[5]; char branch[5];
char buffer[100];
};
fstream file; student s;
void writerecord()
{
file.open("pg3.txt",ios::app); if(!file)
{
cout<<"cannot open the file in append mode"; getch();
exit(1);
}
cout<<"\nenter the student name = "; cin>>s.name;
cout<<"\nenter the usn = "; cin>>s.usn; cout<<"\nenter the age = "; cin>>s.age;
cout<<"\nenter the sem = "; cin>>s.sem;
cout<<"\nenter the branch = "; cin>>s.branch;
strcpy(s.buffer,s.name); strcat(s.buffer,"|"); strcat(s.buffer,s.usn); strcat(s.buffer,"|"); strcat(s.buffer,s.age); strcat(s.buffer,"|"); strcat(s.buffer,s.sem); strcat(s.buffer,"|");
strcat(s.buffer,s.branch);
strcat(s.buffer,"\n"); file<<s.buffer; file.close();
}
void search()
{
char usn[10]; char extra[45];
file.open("pg3.txt",ios::in); if(!file)
{
cout<<"\nunable to open the file in read mode"; getch();
exit(0);
}
cout<<"\nenter the record's usn you want to search = "; cin>>usn;
while(!file.eof())
{
file.getline(s.name,10,'|');
file.getline(s.usn,10,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|'); file.getline(s.branch,5,'\n');
if(strcmp(s.usn,usn)==0)
{
cout<<"\nrecord found"; cout<<"\nname\tusn\tage\tsem\tbranch";
cout<<"\n"<<s.name<<"\t"<<s.usn<<"\t"; cout<<s.age<<"\t"<<s.sem<<"\t"<<s.branch;
file.close();
getch(); return;
}
}
cout<<"\nrecord not found"; file.close();
getch(); return;
}
void displayFile()
{
int i; file.open("pg3.txt",ios::in);
if(!file)
{
cout<<"\ncannot open the file in read mode"; getch();
exit(1);
}
i=0;
printf("\n\nNAME\t\tUSN\t\tAGE\t\tSEM\t\tBRANCH\n");
while(!file.eof())
{
file.getline(s.name,15,'|');
file.getline(s.usn,15,'|');
file.getline(s.age,5,'|');
file.getline(s.sem,5,'|'); file.getline(s.branch,5,'\n');
printf("\n%s\t\t%s\t\t%s\t\t%s\t\t%s",s.name,s.usn,s.age,s.sem,s.branch); i++;
}
file.close();
getch();
}
void modify()
{
char usn[10];
int i; int j;
student s[100];
file.open("pg3.txt",ios::in); if(!file)
{
cout<<"\nunable to open the file in input mode"; getch();
exit(1);
}
cout<<"\nenter the usn "; cin>>usn;
i=0;
while(!file.eof())
{
file.getline(s[i].name,15,'|');
file.getline(s[i].usn,15,'|');
file.getline(s[i].age,5,'|');
file.getline(s[i].sem,5,'|');
file.getline(s[i].branch,5,'\n'); i++;
}
i--;
for(j=0;j<i;j++)
{
if(strcmp(usn,s[j].usn)==0)
{
cout<<"\nthe old values of the record with usn"<<usn<<"are"; cout<<"\nname = "<<s[j].name;
cout<<"\nusn = "<<s[j].usn; cout<<"\nage = "<<s[j].age; cout<<"\nsem = "<<s[j].sem; cout<<"\nbranch = "<<s[j].branch;
cout<<"\nenter the new values\n"; cout<<"\nname = "; cin>>s[j].name;
cout<<"\nusn = "; cin>>s[j].usn; cout<<"\nage = "; cin>>s[j].age; cout<<"\nsem = "; cin>>s[j].sem; cout<<"\nbranch = "; cin>>s[j].branch; break;
}
}
if(j==i)
{
cout<<"\nthe record with usn " <<usn<< "is not present "; getch();
return;
}
file.close();
file.open("pg3.txt",ios::out); if(!file)
{
cout<<"\nunable to open the file in output mode"; getch();
return;
}
for(j=0;j<i;j++)
{
file<<s[j].name<<'|'<<s[j].usn<<'|'<<s[j].age
<<'|'<<s[j].sem<<'|'<<s[j].branch<<'\n';
}
file.close();
}
void main()
{
	int choice; while(1)
{
clrscr();
cout<<"\n RakshithaGK_1dt17is067"; cout<<"\n 0 : exit"; cout<<"\n 1 : write to file";
cout<<"\n 2 : display the file"; cout<<"\n 3 : modify the file"; cout<<"\n 4 : search"; cout<<"\n\n enter the choice : "; cin>>choice;
switch(choice)
{
case 1: writerecord(); break;
case 2: displayFile(); break;
case 3: modify();
break;
case 4: search();
break; case 0: exit(0);
default: cout<<"\ninvalid input..."; break;
}
}
}

 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}