Segmentation Fault in Control Flow Analysis - data-structures

I'm Getting a Segmentation fault here
if((cur->block)!=(cur->next)->block)
I Tried to fix Some on my own but didn't got it correct.
this Program is used to implement Control Flow Analysis it was having some more Segmentation faults which were identified and fixed ,but now. I don't have any alternative to try and edit this code.
struct Listnode
{
char data[50];
int leader,block,u_goto,c_goto;
struct Listnode *next;
char label[10],target[10];
}*temp,*cur,*first=NULL,*last=NULL,*cur1;
FILE *fpr;
void createnode(char code[50])
{
temp=(struct Listnode*) malloc(sizeof(struct Listnode));
strcpy(temp->data,code);
strcpy(temp->label,"\0");
strcpy(temp->target,"\0");
temp->leader=0;
temp->block=0;
temp->u_goto=0;
temp->c_goto=0;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
}
void main()
{
char codeline[50];
char c,dup[50],target[10];
char *substring,*token;
int i=0,j=0,block,block1;
fpr= fopen("P10-CFA.txt","r");
while((c=getc(fpr))!=EOF)
{
if(c!='\n')
{
codeline[i]=c;
i++;
}
else
{
codeline[i]='\0';
createnode(codeline);
i=0;
}
}
//create last node
codeline[i]='\0';
createnode(codeline);
fclose(fpr);
// find out leaders,conditional stmts
cur=first;
cur->leader=1;
while(cur!=NULL)
{
substring=strstr((cur->data),"if");
if(substring==NULL)
{
if((strstr((cur->data),"goto"))!=NULL)
{
cur->u_goto=1;
(cur->next)->leader=1;
}
}
else
{
cur->c_goto=1;
(cur->next)->leader=1;
}
substring=strstr((cur->data),":");
if(substring!=NULL)
{
cur->leader=1;
}
substring=strstr((cur->data),"call");
if(substring!=NULL)
{
cur->leader=1;
}
if(strstr(cur->data,"return")!=NULL)
{
cur->leader=1;
(cur->next)->leader=1;
}
cur=cur->next;
//to find labels and targets
cur=first;
while(cur!=NULL)
{
if((cur->u_goto==1)||(cur->c_goto==1))
{
substring=strstr(cur->data,":");
if(substring!=NULL)
{
token=strstr(substring,"L" );
if(token!=NULL)
strcpy(cur->target,token);
else
{
substring=strstr(cur->data,"L");
if(substring!=NULL)
strcpy(cur->target,substring);
}
}
if(strstr(cur->data,":")!=NULL)
{
strcpy(dup,cur->data);
token=strtok(dup,":");
//printf("\ntoken:%s",token);
if(token!=NULL)
strcpy(cur->label,token);
}
cur=cur->next;
//to identify blocks
cur=first;
while(cur!= NULL)
{
cur=cur->next;
if((cur->leader)==1)
{
j++;
cur->block=j;
}
else
cur->block=j;
}
printf("\n\n.....Basic Blocks \n");
cur=first;
j=0;
printf("\nBlock %d:",j);
while(cur!=NULL)
{
if ((cur->block)==j)
{
printf("%s",cur->data);
printf("\n\t");
cur=cur->next;
}
else
j++;
printf("\nBlock %d:",j);
}
}
//to output the control flow from each block
printf ("\t\t.......Control Flow.........\n\n");
cur=first;
i=0;
while(cur!=NULL)
{
if((cur->block)!=(cur->next)->block)
{
block=cur->block;
if(cur->u_goto==1)
{
strcpy(target,cur->target);
cur1=first;
while(cur1!=NULL)
{
if(strcmp(cur1->label,target)==0)
{
block1=cur1->block;
printf("\t\tBlock%d---->Block%dln",block,block1);
}
cur1=cur1->next;
}
}
else if(cur->c_goto==1)
{
strcpy(target,cur->target);
cur1=first;
while(cur1!=NULL)
{
if(strcmp(cur1->label,target)==0)
{
block1=cur1->block;
printf("lt\1Block%d---TRUE--->Block%d---FALSE--->Block%d\n",block,block1,(block+1));
}
cur1=cur1->next;
}
}
else if(strstr(cur->data,"return")==NULL)
{
printf("\t\tBlock%d--->Block%d\n",block,(block+1));
}
else
printf("lt\tBlock%d--->NULL\n",block);
}
cur=cur->next;
}
cur=last;
block= cur->block;
printf("\t\tBlock%d--->NULL",block);
}
}
}

Related

How to deletenode from BinaryTree?

I tried to write a method to delete Node from BinaryTree from my understanding.
is that method is true or not?!
public void deleteNode(TreeNode oldNode, TreeNode current) {
if(oldNode.data != current.data) {
System.out.print("Error");
return;
}
if(oldNode.data == current.data) {
current = null;
System.out.print("Delete succesfully ");
return;
}
if(oldNode.data > current.data) {
deleteNode(oldNode,current.right);
}
if(oldNode.data < current.data) {
deleteNode(oldNode,current.left);
}
}

SFML empty white window

i've an issue , im writing a menu to the maze game using SFML lib but something went wrong i wrote a code to redner a window and make a menu code. whem im clicking "Local windows debbuger" in vs2017 proggam opens a white window which shoud not suposed to happen. im pretty new at programing and i dont see all mistakes i made in this code but for me it's written right.
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
using namespace sf;
#define Max_main_menu 4
class Menu
{
public:
Menu(float width, float height);
void draw(RenderWindow& window);
void MoveUp();
void MoveDown();
int MenuPressed()
{
return MenuSelected;
}
~Menu();
private:
int MenuSelected;
Text menu[Max_main_menu];
};
menu.h
_____________________________
#include "Menu.h"
Menu::Menu(float widht, float height)
{
//Graj
menu[0].setFillColor(Color::White);
menu[0].setString("Graj");
menu[0].setCharacterSize(70);
menu[0].setPosition(600, 300);
menu[1].setFillColor(Color::White);
menu[1].setString("Opcje");
menu[1].setCharacterSize(60);
menu[1].setPosition(500, 300);
menu[2].setFillColor(Color::White);
menu[2].setString("Autorzy");
menu[2].setCharacterSize(60);
menu[2].setPosition(400, 300);
menu[3].setFillColor(Color::White);
menu[3].setString("Najlepszy Csas");
menu[3].setCharacterSize(60);
menu[3].setPosition(300, 300);
MenuSelected = -1;
}
Menu::~Menu()
{
}
void Menu::draw(RenderWindow& window) {
for (int i = 0; i < Max_main_menu; i++) {
window.draw(menu[i]);
}
}
void Menu::MoveUp() {
if (MenuSelected - 1 >= 0) {
menu[MenuSelected].setFillColor(Color::White);
MenuSelected--;
if (MenuSelected == -1) {
MenuSelected = 2;
}
menu[MenuSelected].setFillColor(Color::Blue);
}
}
void Menu::MoveDown()
{
if (MenuSelected + 1 <= Max_main_menu)
{
menu[MenuSelected].setFillColor(Color::White);
MenuSelected++;
if (MenuSelected == 4)
{
MenuSelected = 0;
}
menu[MenuSelected].setFillColor(Color::Blue);
}
}
menu.cpp
______________________________________________________
#include <SFML/Graphics.hpp>
#include "D:\Proejkt\Menu.h"
int main()
{
RenderWindow MENU(VideoMode(1280, 720), "Labirynt", Style::Default);
Menu menu(MENU.getSize().x, MENU.getSize().y);
while (MENU.isOpen())
{
Event event;
while (MENU.pollEvent(event))
{
if (event.type == Event::Closed)
{
MENU.close();
}
if (event.type == Event::KeyReleased)
{
if (event.key.code == Keyboard::Up)
{
menu.MoveUp();
break;
}
if (event.key.code == Keyboard::Down)
{
menu.MoveDown();
break;
}
if (event.key.code == Keyboard::Return)
{
RenderWindow Graj(VideoMode(1280, 720), "Labirynt");
RenderWindow Opcje(VideoMode(1280, 720), "Opcje");
RenderWindow Autorzy(VideoMode(1280, 720), "Autorzy");
RenderWindow NajlepszyCzas(VideoMode(1280, 720), "Najlepszy Czas");
int x = menu.MenuPressed();
if (x == 0)
{
while (Graj.isOpen())
{
Event aevent;
while (Graj.pollEvent(aevent))
{
if (aevent.type == Event::Closed)
{
Graj.close();
}
if (aevent.type == Event::KeyPressed)
{
if (aevent.key.code == Keyboard::Escape)
{
Graj.close();
}
}
}
}
Opcje.close();
Autorzy.close();
NajlepszyCzas.close();
Graj.clear();
Graj.display();
}
if (x == 1)
{
while (Opcje.isOpen())
{
Event aevent;
while (Opcje.pollEvent(aevent))
{
if (aevent.type == Event::Closed)
{
Opcje.close();
}
if (aevent.type == Event::KeyPressed)
{
if (aevent.key.code == Keyboard::Escape)
{
Opcje.close();
}
}
}
}
Graj.close();
Autorzy.close();
NajlepszyCzas.close();
Opcje.clear();
Opcje.display();
}
}
}
}
}
MENU.clear();
menu.draw(MENU);
MENU.display();
}
project.cpp
Your Main.clear(), Main.draw() and Main.display() is not in a while loop. Pls put them in a while loop.

deriving time-complexity of nqueens problem

Can anyone prove/derive the time complexity of my nqueens solution approach?
I am going through each and every position on the grid and if it is possible to place a queen there, then, I am calculating solution by first placing the queen and then unplacing the queen, else I move on.
Code:
bool notinrow(int row,int col,vector<string> tra)
{
for(int i=0;i<tra.size();i++)
{
if(tra[row][i]=='Q' & i!=col)
return false;
}
return true;
}
bool notincol(int row,int col,vector<string> tra)
{
for(int j=0;j<tra.size();j++)
{
if(tra[j][col]=='Q' & j!=row)
return false;
}
return true;
}
bool notindiag1(int r,int c, vector<string> tra)
{
int i=r-1;
int j=c-1;
while(i>=0 & j>=0)
{
if(tra[i][j]=='Q')
return false;
i--;j--;
}
return true;
}
bool notindiag2(int r, int c,vector<string> tra)
{ int i=r-1;
int j=c+1;
while(i>=0 & j<totqueens)
{
if(tra[i][j]=='Q')
return false;
i--;j++;
}
return true;
}
void nqueens(int number,vector<string> tra,int currqueens)
{
if(currqueens==totqueens)
{
bhej.push_back(tra);
return ;
}
if(number==totiter)
return;
int x=number/totqueens;
int y=number%totqueens;
if(ispossible(x,y,tra))
{
tra[x][y]='Q';
nqueens(number+1,tra,currqueens+1);
tra[x][y]='.';
nqueens(number+1,tra,currqueens) ;
}
else
nqueens(number+1,tra,currqueens);
}```

how can this program give a floating point error?

This is a "sort of" logical question that i tried to solve using c.
I basically input all the divisors of a number in an array and the added the digits to a single digit number and stored in a variable 'best'.
But the output that i get is "Floating point exception (core dumped)"
Thought it would be a problem with the one of the loops, but i cant find any.
could i please know what is the meaning of this error and whats causing it ?
#include <stdio.h>
void main()
{
int n,a[50],b[50],k=0,l1=0,l2=0,temp,big,bigi,best=0,i,l3;
printf("Enter an integer (less than 10^5):- ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
b[i]=a[i];
}
for(i=0;i<k;i++)
{
if(a[i]<10)
{
continue;
}
else
{
l3=a[i];
while(l3>0)
{
temp=l3%10;
l1=l1+temp;
l3=l3/10;
}
if(l1>=10)
{
while(l1>0)
{
temp=l1%10;
l2=l2+temp;
l1=l1/10;
}
}
a[i]=l2;
}
}
big=a[0];
for(i=0;i<k;i++)
{
if(a[i]>big)
{
big=a[i];
bigi=i;
}
}
for(i=0;i<k;i++)
{
while(i!=bigi)
{
if(a[bigi]==a[i])
{
if(b[bigi]>b[i])
{
best=a[i];
}
else
{
best=a[bigi];
}
}
}
if(best=0)
{
best=a[bigi];
}
}
printf("The best number is :- %d",best);
}
Oh gosh Steve, your code formatting!
I don't have an answer, but I'm willing to bet it has something to do with the array indices. You are creating an array with 50 elements.
Then you are accessing it with an index of k that is dependent on your input! How can you guarantee that k will not be greater than 49?
Put some debug printfs in there. Everywhere. Monitor your variable k. Look at what your loops are doing, and where you are getting stuck.
And for my own sanity, and that of anyone else reading this, here:
#include <stdio.h>
void main()
{
int n,a[50],b[50],k=0,l1=0,l2=0,temp,big,bigi,best=0,i,l3;
printf("Enter an integer (less than 10^5):- ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
{
a[k]=i;
k++;
}
}
for(i=0;i<k;i++)
{
b[i]=a[i];
}
for(i=0;i<k;i++)
{
if(a[i]<10)
{
continue;
}
else
{
l3=a[i];
while(l3>0)
{
temp=l3%10;
l1=l1+temp;
l3=l3/10;
}
if(l1>=10)
{
while(l1>0)
{
temp=l1%10;
l2=l2+temp;
l1=l1/10;
}
}
a[i]=l2;
}
}
big=a[0];
for(i=0;i<k;i++)
{
if(a[i]>big)
{
big=a[i];
bigi=i;
}
}
for(i=0;i<k;i++)
{
while(i!=bigi)
{
if(a[bigi]==a[i])
{
if(b[bigi]>b[i])
{
best=a[i];
}
else
{
best=a[bigi];
}
}
}
if(best=0)
{
best=a[bigi];
}
}
printf("The best number is :- %d",best);
}

File Handling issue in C++

In the menu(), in case 3 and 4, modification of object information is to be done. The modification is happening but afterwards the temporary file is not being removed and the temporary file is not being renamed to the main file - "userifo.dat". Therefore whenever the program is rerun the data is inaccessibly. Is the problem not what I understand it to be??
Would love any other off-topic suggestions for improvement.
Thanks
#include<iostream>
#include<string.h>
#include<fstream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
using namespace std;
/**************************************************************************/
struct uinfo
{
char uname[50],pass[50],secq[300],seca[300];
float mbill;
int sccode[30],scno;
void newinfo()
{
system("cls");
cout<<"Enter Username : ";
cin.ignore();
cin.getline(uname,50);
cout<<"Enter Password : ";
cin.getline(pass,50);
cout<<"Enter a security question : ";
cin.getline(secq,300);
cout<<"Enter Answer to security answer : ";
cin.getline(seca,300);
mbill=0;
scno=0;
}
}userobj;
/**************************************************************************/
struct cinfo
{
char cname[30];
int ccode;
float price;
void display()
{
cout<<"Channel name : "<<cname<<"\nPrice : "<<price<<"\nCode : "<<ccode<<endl<<endl;
}
void input()
{
system("cls");
cout<<"Enter Name Of Channel : ";
cin.ignore();
cin.getline(cname,30);
cout<<"Enter price of subscription : ";
cin>>price;
cout<<"Enter The channel code : ";
cin>>ccode;
}
}chobj;
/**************************************************************************/
void adminmenu()
{
int cho=-1;
uinfo uobj;
while(cho!=0)
{
system("cls");
cout<<"\t\t\tWelcome to the Admin Menu\n\n"
<<"Enter what you want to do : \n"
<<"1. View the usernames and the"
<<" respective Monthly bill of all Users\n"
<<"2. Remove a user\n"
<<"3. Add a channel\n"
<<"4. Modify the price of a channel\n(It will change the Monthly Bill of all"
<<" users who have subscribed the channel)\n"
<<"0. Back to normal Menu\n";
cin>>cho;
switch(cho)
{
case 1 : {
ifstream file;
file.open("userifo.dat",ios::binary);
while(file.read((char*)&uobj,sizeof(uobj)))
{
cout<<"User : "<<uobj.uname<<"\nMonthly Bill : "<<uobj.mbill<<"\n\n";
}
getch();
break;
}
case 2 : {
char tempusern[50];
int flag=0;
cout<<"Enter the username of the user whose account is to be deleted : ";
cin.ignore();
cin.getline(tempusern,50);
ifstream file;
file.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary);
while(file.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(tempusern,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
flag=1;
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
if(flag==0)
{
cout<<"No Such User....";
}
else
cout<<"User Deleted";
getch();
break;
}
case 3 : {
cinfo tempcobj;
tempcobj.input();
ofstream file;
file.open("channelinfo.dat",ios::binary|ios::app);
file.write((char*)&tempcobj,sizeof(tempcobj));
file.close();
break;
}
case 4 : {
system("cls");
ifstream file1;
file1.open("channelinfo.dat",ios::binary);
while(file1.read((char*)&chobj,sizeof(chobj)))
{
chobj.display();
}
file1.close();
cinfo tempcobj;
int tempcode,flag=0;
float cmbill,oprice;
cout<<"Enter the code of the channel that is to be updated : ";
cin>>tempcode;
ifstream file;
file.open("channelinfo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary);
while(file.read((char*)&tempcobj,sizeof(tempcobj)))
{
oprice=tempcobj.price;
if(tempcobj.ccode!=tempcode)
{
temp.write((char*)&tempcobj,sizeof(tempcobj));
}
else
{
flag=1;
cout<<"Enter the new price of the channel : ";
cin>>tempcobj.price;
temp.write((char*)&tempcobj,sizeof(tempcobj));
fstream cprice;
cprice.open("userifo.dat",ios::binary|ios::app);
ofstream temp2;
temp2.open("temp2.dat",ios::binary|ios::app);
while(cprice.read((char*)&uobj,sizeof(uobj)))
{
for(int i=0;i<uobj.scno;++i)
{
if(tempcode==uobj.sccode[i])
uobj.mbill+=(oprice-tempcobj.price);
}
temp2.write((char*)&uobj,sizeof(uobj));
}
cprice.close();
temp2.close();
remove("userifo.dat");
rename("temp2.dat","userifo.dat");
}
}
if(flag==0)
{
cout<<"No Such Refferal Code !!! Plaes Try Again....";
getch();
}
file.close();
temp.close();
remove("channelinfo.dat");
rename("temp.dat","channelinfo.dat");
break;
}
case 0 : break;
default: cout<<"You've Entered a worng choice!!! Please Try Again....";
}
}
}
/************************************************************************/
void show_subchannel()
{
int i=0;
ifstream file;
for (i;i<userobj.scno;++i) {
file.open("channelinfo.dat",ios::binary);
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(chobj.ccode==userobj.sccode[i])
chobj.display();
}
file.close();
}
getch();
}
/*************************************************************************/
void menu()
{
system("cls");
int choice=-1;
while(choice!=0)
{
system("cls");
cout<<"\t\t\t\tWelcome to the JC TV services menu\n\n\n"
<<"Enter your choice as to how you want to proceed -\n"
<<"1.View your Current Monthly Bill\n"
<<"2.View your Subscribed Channel list\n"
<<"3.Subscribe to a new Channel\n"
<<"4.Un-subscribe to a Channel\n"
<<"5.Enter into Administrator Mode\n"
<<"0.Log out and Exit\n";
cin>>choice;
switch(choice)
{
case 2:{
if(userobj.scno!=0)
{
show_subchannel();
}
else
cout<<"No Existing channels";
getch();
} break;
case 1:{
cout<<"Your Current Monthly Bill is : "<<userobj.mbill; getch();
}break;
case 3:{
ifstream file;
file.open("channelinfo.dat",ios::binary);
while(file.read((char*)&chobj,sizeof(chobj)))
{
chobj.display();
}
int tempcode;float addsum=0;
int flag=0;
for(;flag==0;)
{
cout<<"\nEnter the code of the channel you want to add : ";
cin>>tempcode;
for(int i=0;i<=userobj.scno;++i)
{
if(tempcode==userobj.sccode[i])
{
cout<<"Channel Already Subscribed.Please Enter another code";
break;
}
flag=1;
}
}
file.close();
file.open("channelinfo.dat",ios::binary);
int f=1;
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(tempcode==chobj.ccode)
{
addsum=chobj.price;
userobj.mbill+=addsum;
userobj.sccode[userobj.scno]=tempcode;
userobj.scno++;
uinfo uobj;
ifstream umfile;
umfile.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary|ios::app);
while(umfile.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(userobj.uname,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
{
temp.write((char*)&userobj,sizeof(userobj));
}
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
f=0;
}
}
if(f==1)
{
cout<<"No such refferal code...";getch();
}
}break;
case 4:{
show_subchannel();
int flag=1;
cout<<"Enter the code of the channel you want to un-subscribe to : ";
int tempcode;
cin>>tempcode;
for(int i=0;i<userobj.scno;++i)
{
if(tempcode==userobj.sccode[i])
{
flag=0;
for(int j=i;j<userobj.scno-1;++j)
userobj.sccode[j]=userobj.sccode[j+1];
userobj.scno--;
ifstream file;
file.open("channelinfo.dat",ios::binary);
float subsum;
while(file.read((char*)&chobj,sizeof(chobj)))
{
if(tempcode==chobj.ccode){
subsum=chobj.price;
userobj.mbill-=subsum;
userobj.scno--;
}
file.close();
uinfo uobj;
ifstream umfile;
umfile.open("userifo.dat",ios::binary);
ofstream temp;
temp.open("temp.dat",ios::binary|ios::app);
while(umfile.read((char*)&uobj,sizeof(uobj)))
{
if(strcmpi(userobj.uname,uobj.uname)!=0)
{
temp.write((char*)&uobj,sizeof(uobj));
}
else
{
temp.write((char*)&userobj,sizeof(userobj));
}
}
file.close();
temp.close();
remove("userifo.dat");
rename("temp.dat","userifo.dat");
}
}
if(flag==1)
{ cout<<"No such Referal Code"; getch(); }
}
}break;
case 5:{
char apass[50];
cout<<"Enter the Admin Special Password : ";
cin.ignore();
cin.getline(apass,50);
if (strcmpi(apass,"go for it")==0) {
adminmenu();
}
else
{ cout<<"Wrong Password...."; getch(); }
}break;
case 0: exit(0); break;
default:{
cout<<"Invaild Choice...";
}
}
}
}
/*************************************************************************/
void login()
{
system("cls");
char usernametemp[50],passwordtemp[50];
cout<<"Enter Username : ";
cin.ignore();
cin.getline(usernametemp,50);
cout<<"Enter Password : ";
cin.getline(passwordtemp,50);
int flag=0;
ifstream loginfile;
loginfile.open("userifo.dat",ios::binary);
while (loginfile.read((char*)&userobj,sizeof(userobj))) {
if (strcmpi(userobj.uname,usernametemp)==0) {
if (strcmpi(userobj.pass,passwordtemp)==0) {
flag=1;
menu();
}
else {
cout<<"Wrong password!!!";
cout<<"\nAnswer the following Question : \n"<<userobj.secq<<endl;
char secqa[300];
cin.getline(secqa,300);
if (strcmpi(secqa,userobj.seca)==0) {
flag=1;
menu();
}
else{
cout<<"Wrong Answer!!!! Try Again.....";
getch();
login();
}
}
}
}
if (flag==0) {
{std::cout << "No Such Username Exists!!! Try again...";
getch();
login();
}
}
loginfile.close();
}
/*************************************************************************/
void newuser()
{
int flag=1;
uinfo tempobj,tempobj2;
while(1)
{
tempobj.newinfo() ;
ifstream nufile;
nufile.open("userifo.dat",ios::binary|ios::in);
while(nufile.read((char*)&tempobj2,sizeof(tempobj2)))
{
if(strcmpi(tempobj.uname,tempobj2.uname)==0)
{
flag=0;
cout<<"Username already exists!!! Try again...";
getch();
nufile.close();
break;
}
else
flag=1;
}
if(flag==1)
{
ofstream nu1file;
nu1file.open("userifo.dat",ios::binary|ios::app);
nu1file.write((char*)&tempobj,sizeof(tempobj));
cout<<"User Created";
userobj=tempobj;
getch();
nu1file.close();
menu();
break;
}
}
}
/*************************************************************************/
int main()
{
char ch;
cout<<"\t\t\t WELCOME\n\n"
<<"Are you a new User (Y/N) : ";
cin>>ch;
if(toupper(ch)=='Y')
{
int k=1;
while(k=1)
{
newuser();
}
}
else
{
login();
}
return 0;
}
/*************************************************************************/

Resources