I want to know why I can't print record[i][1] using
puts (record[i][1]);
If I do:
cout<< record[i][1];
Then only the first letter of the string appears, not the rest.
This is for a school project.
I code on Turbo C++ (TC4).
#include< fstream.h>
#include< strstream.h>
#include< conio.h>
#include< dos.h>
#include< graphics.h>
#include< string.h>
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
#include< ctype.h>
#include< iomanip.h>
#include< iostream.h>
char record[500][5]; //array to store 500 entries each consisting of
//name,cell no, agesex, crime and sentenced.
int cricode=1;
char name[35];
char criminalno[10];
char agesex[5];
char sentenced[3];
char crime[20];
char passwordU[30];
int checkpass(char[]);
void menu(int);
void advmenu(int);
void inputdata();
void report(int) ;
void searchrecN(char [][5], int);
void searchrecC(char [][5], int);
void selectcrime(char []);
void changepass();
void displaydata(int);
void main()
{
clrscr();
ifstream fp;
fp.open("criminal.txt");
do
{char a;
fp.getline(&record[cricode][1],30,'!') ; //name
fp.getline(&record[cricode][2],10,'#'); //cellno
fp.getline(&record[cricode][3],7,'%'); //agesex
fp.getline(&record[cricode][4],20,'^'); //crime
fp.getline(&record[cricode][5],3,'\n'); //sentenced
cricode++ ;
}while (fp.eof() == 0);
cricode--; //final value of cricode
fp.close();
menu(cricode);
getch();
}
void menu(int code)
{
fstream fp;
int hell;
clrscr();
cout<<"\n\n";
cout<<"______________________________________\n";
cout<<" L O C A L M E N U \n";
cout<<"______________________________________\n";
cout<<"\n\n";
cout<<"\t\t\t"<<"\n\n[1] Create a Record.";
cout<<"\t\t\t"<<"\n\n[2] Search for a record.";
cout<<"\t\t\t"<<"\n\n[3] Advance Options.";
cout<<"\t\t\t"<<"\n\n[4] Display all entries.";
cout<<"\t\t\t"<<"\n\n[5] Exit \n :";
cin>>hell;
switch(hell)
{
case 1: clrscr();
inputdata();
system("pause");
menu(code);
break;
case 2: clrscr();
char na[30];
int ch;
clrscr();
cout<<"\t\t\t"<<"\nSearch by -> ";
cout<<"\t\t\t\t"<<"\n[1] Name : ";
cout<<"\t\t\t\t"<<"\n[2] Cell No. : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Please Enter Name : ";
//gets(na);
searchrecN(record,code);
break;
case 2:
cout<<"\n Please Enter Cell No. : ";
//gets(na);
searchrecC(record,code);
break;
default : cout<<"invalid";
}
menu(code);
break;
case 4: clrscr();
for(int i=0;i<code;i++)
{
cout<<"\nName \t Cellno \t age/sex \t crime \t sentenced \n";
cout<<record[i][1]; //only one character is displayed of the
// entire word.
// if i do
//puts(record[i][1]); // Error: cannot convert 'int' to 'const char *'
}
cout<<"\n";
system("pause");
menu(code);
break;
case 5: //clrscr();
//intro();
break;
default:
cout<<"\a";
}
}
int i;
void report(int cc)
{
cout <<record[cc][1]<<"\t"
<<record[cc][2]<<"\t"
<<record[cc][3]<<"\t"
<<record[cc][4]<<"\t"
<<record[cc][5]<<"\n";
}
void inputdata()
{
char ch;
char recname[30],reccell[10],recagex[7],recrime[20],recsent[3];
ofstream fp;
fp.open("criminal.txt", ios::app);
do
{
cout<<"\n[.] Name : ";
gets(recname);
cout<<"\n[.] Criminal Number : ";
gets(reccell);
cout<<"\n[.] Age/Sex {eg: 21/M} : ";
gets(recagex);
cout<<"\n[.] Select Crime : ";
selectcrime(recrime);
cout<<"\n[.] Sentenced For : ";
gets(recsent);
fp <<recname
<<"!"<<reccell
<<"#"<<recagex
<<"%"<<recrime
<<"^"<<recsent
<<"\n";
cout<<"Do you Want to Continue(Y/N): ";
cin>>ch;
}while((ch=='y')||(ch=='Y'));
system("pause");
}
void searchrecN(char record[][5], int cricode)
{
int cnt;
char search[30];
cout<<"Enter Cellno.: ";
gets(search);
for(i=0;i<cricode;i++)
{
if (strcmpi(search,&record[i][1])==0)
{
clrscr();
cout<<"\nRecord Found.";
cout<<"\nLoading Info......";
system("pause");
report(i);
cnt=1;
break;
}
}
if(cnt==0)
{
clrscr();
cout<<"\n ***Record does not exist*** ";
cout<<"\n ***Redirecting***\n";
system("pause");
}
}
void searchrecC(char record[][5], int cricode)
{
int cnt;
char search[10];
cout<<"Enter Cellno.: ";
gets(search);
for(i=0;i<cricode;i++)
{
if (strcmpi(search,&record[i][2])==0)
{
clrscr();
cout<<"\nRecord Found.";
cout<<"\nLoading Info......";
system("pause");
report(i);
cnt=1;
break;
}
}
if(cnt==0)
{
clrscr();
cout<<"\n ***Record does not exist*** ";
cout<<"\n ***Redirecting***\n";
system("pause");
}
}
void displaydata(int c)
{
clrscr();
fstream fp;
for(i=0;i<c;i++)
{
cout<<"Name\tCellno\tage/sex\tcrime\tsentenced";
cout <<record[i][1]<<"\t"<<record[i][2]<<"\t"
<<record[i][3]<<"\t"<<record[i][4]<<"\t"
<<record[i][5]<<"\n";
}
/* cout<<setw(100)<<"\n[.] Criminal No. : ";
puts(record[i][2]);
cout<<setw(100)<<"\n[.] Age/Sex : ";
puts(record[i][3]);
cout<<setw(100)<<"\n[.] Alleged for : : ";
// puts(obj[i].category);
cout<<setw(100)<<"\n[.] Crime : ";
puts(record[i][4]);
// cout<<setw(100)<<"\n[.] Expenses per month : ";
puts(record[i][4]);
/* cout<<setw(100)<<"\n[.] Expenses per Month : ";
cout<<exp;
cout<<setw(100)<<"\n[.] Sentenced for : ";
puts(record[i][5]); */
}
Your record array is an array of 500 rows of 5 single characters, not 5 strings.
I recommend you model your data with a structure:
struct Record
{
char name[35];
char criminalno[10];
char agesex[5];
char sentenced[3];
char crime[20];
char passwordU[30];
};
Your database can then be 500 records:
Record database[500];
FYI, safer and less troublesome techniques are to use std::string and std::vector.
Related
I'm working on a project in our programming class which requires me to make an inventory system. There is an error in the code in line 243 that gives weird errors when I compile it.
Line 243 beneath in the code is
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y);
void add();
void view();
void menu();
void types();
void input();
void stock();
void find_name();
void find_number();
void find_type();
void find_mtype();
void all();
void output();
struct inventory{
string prodtype,menutype;
string prodnum,prodname;
int quantity;
float prodprice;
}rec;
int main()
{
int x=1,y=1; //for gotoXY
char l; //choice to determine add/view
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t INVENTORY SYSTEM\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\t\t ---- [1] Add new product ----"
<<"\n\t\t ---- [2] Add stocks to existing products ----"
<<"\n\t\t ---- [3] View Products ----"
<<"\n\t\t ---- [4] Exit Program ----"
<<"\n\n\t\t\t\t Enter choice : ";
back:
l=getch();
if(l=='1')
add();
else if(l=='2')
stock();
else if(l=='3')
view();
else if(l=='4')
return 0;
else
goto back;
}
void stock_intro()
{
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t ADDING OF STOCKS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n\n";
}
void stock()
{
string num; //for inputting prodnum...
int x; //for adding stocks
string prodtype,menutype;
string prodnum,prodname;
int quantity;
float prodprice;
ifstream inventory("inventory.txt");
system("cls");
stock_intro();
cout<<"\t\t\t Enter Product Number to add stock:\n\n";
cout<<"\t\t\t ";
cin>>num;
while(inventory>>prodtype>>menutype
>>prodnum>>prodname>>quantity
>>prodprice){
system("cls");
cout<<"How many stocks would you like to add?: ";
cin>>x;
rec.quantity=rec.quantity+x;
}
}
void types()
{
ofstream inventory("inventory.txt", ios::app);
char c;
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tFOOD AND BEVERAGES\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
cout<<"\t\t\t\t [A] SNACKS\n\t\t\t\t [B] CANDIES\n\t\t\t\t [C] BISCUITS\n\t\t\t\t [D] CHOCOLATES\n\t\t\t\t [E] CANNED GOODS\n\t\t\t\t [F] CONDIMENTS\n\t\t\t\t [G] INSTANT NOODLES\n\t\t\t\t [H] BEVERAGE / DRINKS\n";
cout<<"\n\t\t\t\t Enter Choice: ";
back:
c=getch();
c=toupper(c);
if(c=='A')
rec.menutype="SNACKS";
else if(c=='B')
rec.menutype="CANDIES";
else if(c=='C')
rec.menutype="BISCUITS";
else if(c=='D')
rec.menutype="CHOCOLATES";
else if(c=='E')
rec.menutype="CANNED GOODS";
else if(c=='F')
rec.menutype="CONDIMENTS";
else if(c=='G')
rec.menutype="INSTANT NOODLES";
else if(c=='H')
rec.menutype="BEVERAGE / DRINKS";
else
goto back;
inventory<<rec.menutype;
}
void input()
{
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tADDING OF PRODUCTS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n\n";
ofstream inventory("inventory.txt", ios::app);
cout<<"\t\t\tEnter Product number: ";
cin>>rec.prodnum;
cout<<"\t\t\tEnter Product name: ";
cin>>rec.prodname;
cout<<"\t\t\tEnter Product price: ";
cin>>rec.prodprice;
cout<<"\t\t\tEnter Quantity: ";
cin>>rec.quantity;
inventory<<endl
<<rec.prodnum<<endl
<<rec.prodname<<endl
<<rec.prodprice<<endl
<<rec.quantity<<endl<<endl;
}
void add()
{
char c,d; //for choosing food or hygene
char e; //for choosing if add another of not
back2:
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t ADD PRODUCTS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
ofstream inventory ("inventory.txt", ios::app);
cout<<"\n\n\t\t\t\t[A] FOOD and BEVERAGES\n\t\t\t\t[B] HYGIENE";
cout<<"\n\n\t\t\t\tEnter Choice: ";
back1:
c=getch();
d=toupper(c);
if(d=='A')
rec.prodtype="Food & Beverages";
else if(d=='B')
rec.prodtype="Hygiene";
else
goto back1;
system("cls");
inventory<<rec.prodtype<<endl;
types();
input();
cout<<"\n\t\t\tEnter another product? [Y / N]: ";
back3:
e=getch();
e=toupper(e);
if(e=='Y')
goto back2;
else if(e=='N')
main();
else
goto back3;
}
void output()
{
cout<<"Product Type: "
<<rec.prodtype<<endl;
cout<<"Menu Type : "
<<rec.menutype<<endl;
cout<<" Product Number: "
<<rec.prodnum<<endl;
cout<<" Product Name: "
<<rec.prodname<<endl;
cout<<" Product Price: "
<<rec.prodprice<<endl;
cout<<" Stocks Available: "
<<rec.quantity<<endl;
}
void view_intro()
{
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tVIEWING OF INVENTORY\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
}
void view()
{
char ch;
system("cls");
view_intro();
cout<<"\n\t\t ---- [1] Find by Product Name ----"
<<"\n\t\t ---- [2] Find by Product Number ----"
<<"\n\t\t ---- [3] Find by Product Type ----"
<<"\n\t\t ---- [4] Find by Menu Type ----"
<<"\n\t\t ---- [5] Display all Records ----"
<<"\n\n\t\t\t\t Enter choice : ";
back:
ch=getch();
ch=getch();
switch(ch)
{
case '1': find_name();break;
case '2': find_number();break;
case '3': find_type();break;
case '4': find_mtype();break;
case '5': all();break;
default: goto back;
}
}
void find_name()
{
string name;
int v=0;
system("cls");
view_intro();
ifstream student("record.txt");
cout<<"\n Enter Product Name [incorrect capitalization is invalid]: ";
cin>>name;
system("cls");
view_intro();
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
>>rec.prodname>>rec.prodprice>>rec.quantity)
{
back:
while(name==rec.prodname)
{
output();
break;
}
v++;
if(v<1)
{
goto back;
}
}
system("pause");
main();
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}
In find_name() function:
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
>>rec.prodname>>rec.prodprice>>rec.quantity)
You dont declare what inventory is in this function. Did you mean to open a file in a variable called inventory like you did in the other functions?
The reason the compiler is giving weird errors is you declared a type called inventory and so not seeing a local variable called inventory it assumes you're using the type which makes no sense in the context of the above line and the compiler gets confused and gives a fairly obscure error.
Why is the following code not giving results and how to get results?
Whenever I run the code, it first asks for the names of the players of two teams playing the match, then it shows the menu from which if we select any one of the option it again asks for the batsman name which is not according to the program designed. My research on the code and the problem is that I think buffer memory is full but I don't know how to free it, any help would be beneficial. Thank you
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class scorecard{
char batname[11][20];
int runscored[11];
char situation[11][10];
char mode[11][15];
char bowlername[11][20];
float oversplayed[11];
int maiden[11];
int runsgiven[11];
int wicketstaken[11];
public:
void updatebatsman(void);
void updatebowler(void);
void displaybat(void);
void displaybowl(void);
void menu(void);
scorecard()
{for(int n=0;n<12;n++)
{
runscored[n]={0};
oversplayed[n]={0};
maiden[n]={0};
runsgiven[n]={0};
wicketstaken[n]={0};
}
}
};
int main()
{
int jb=0;
scorecard s1;
int kb;
s1.menu();
do
{
cout<< "Enter the option"<<endl;
cout<<"(1) Display batting score"<<endl<<"(2) Display Bowling score"<<endl<<"(3) Update batting score"<<endl;
cout<<"(4) Update Bowling score"<<endl;
cin >>kb;
switch(kb)
{
case 1 : s1.displaybat();
break;
case 2 :s1.displaybowl();break;
case 3:s1.updatebatsman();break;
case 4:s1.updatebowler();break;
default:cout<<"Wrong choice";
}
}while (jb<1);
}
void scorecard::updatebowler(void)
{char bowlname[20];
int str,k,option,overnumbers,maidenumb,uprun,upwicket;
cout<<"Enter Bowler name:";
cin.getline(bowlname,20);
for( k=0;k<11;k++)
{str= strcmp(bowlername[k],bowlname);
if (str== 0)
{
cout<<"Menu for Bowler information update "<<endl;
cout<<"(1) Update Number of overs"<<endl<<"(2) Update maiden overs"<<endl<<"(3) Update runs given"<<endl;
cout<<"(4) Update wickets taken"<<endl;
cin >> option;
switch(option)
{
case 1:{cout<<"Enter Numbers of overs to be updated:";
cin >>overnumbers;
cout<<endl;
oversplayed[k]+=overnumbers;
break;
}
case 2:{cout <<"Enter the number of maiden overs to be updated:";
cin>>maidenumb;
cout<<endl;
maiden[k]+=maidenumb;
break;
}
case 3:{cout <<"Enter the number of runs to be added:";
cin>>uprun;
cout<<endl;
runsgiven[k]+=uprun;
break;
}
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
}
default:cout<<"wroung choice";
}
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::updatebatsman(void)
{char batsmaname[20];
int str,k;
cout<<"Enter Batsman name:";
cin.getline(batsmaname,20);
for( k=0;k<11;k++)
{str= strcmp(batname[k],batsmaname);
if (str== 0)
{
cout<<"enter runs scored:";
cin>>runscored[k];
cout<<endl<<"enter weather out or not out:";
cin>>situation[k];
cout<<endl<<"enter mode(if batsman out) by which batsman was out:";
cin>>mode[k];
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::displaybat(void)
{
cout << "Batsman name"<<'t'<<"Runs scored"<<'t'<<"situation"<<'t'<<"mode"<<endl;
for(int j=0;j++;j<12)
{
cout<<batname[j]<<'t'<<runscored[j]<<'t'<<situation[j]<<'t'<<mode[j]<<endl;
}
}
void scorecard::displaybowl(void)
{
cout << "Bowler name"<<'t'<<"overs played"<<'t'<<"maiden overs"<<'t'<<"wicket taken"<<'t'<<"Runs given"<<endl;
cout<<endl;
for(int j=0;j++;j<12)
{
cout<<bowlername[j]<<'t'<<oversplayed[j]<<'t'<<maiden[j]<<'t'<<wicketstaken[j]<<'t'<<runsgiven[j]<<endl;
}
}
void scorecard::menu(void)
{
cout<<"Enter the name of players of batting team"<<endl;
for (int k=0;k<11;k++)
{
cout <<"Enter name of player "<<k+1<<":";
cin>>batname[k];
}
cout <<"Enter the name of players of bowling team"<<endl;
for (int n=0;n<11;n++)
{
cout <<"Enter name of player "<<n+1<<":";
cin>>bowlername[n];
}
}
This is very wrong:
for(int j=0;j++;j<12)
It should be:
for(int j=0; j < 11; j++)
You are also missing a break in your case 4 statement for the options:
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
break;
}
default:cout<<"wroung choice";
Without the break you will see also the output wrong choice when the user selects option 4.
My IDE says "Errors exists in required project. Continue to launch?". at 48% through launch attempt. But doesn't show any highlighted errors.
Im using Eclipse indigo release 2.
Some other user said this something similar happened to them, and that they had deleted files from their hard drive recently, which i did but nothing in regards eclipse or c++.
#include <iostream>
#include <string>
using namespace std;
void search(char wrd2[26]);
bool srch(char wrd[26], char ch);
int pos(char wrd[26], char ch);
int cntr(char wrd[26], char ch);
void revr(char wrd2[26], char wrd1[26]);
void chcs(char wrd2[26], char wrd1[26]);
void re_ch(char wrd2[26], char wrd1[26]);
int main()
{
char wrd1[26], wrd2[26];
int opt=0;
cout<<"Please enter two word words."<<endl;
cout<<""<<endl;
cout<<"Please insert your first word here:";
cin>>wrd1;
cout<<""<<endl;
cout<<"Enter your second word here :";
cin>>wrd2;
cout<<endl;
do{
cout<<"Search enter 1:"<<endl;
cout<<endl;
cout<<"Reverse enter 2:"<<endl;
cout<<endl;
cout<<"Change the case enter 3:"<<endl;
cout<<endl;
cout<<"Reverse and change the case enter 4:"<<endl;
cout<<endl;
cout<<"Change your first word enter 5:"<<endl;
cout<<endl;
cout<<"Change your second word enter 6:"<<endl;
cout<<endl;
cout<<"Quit enter 7:"<<endl;
cin>>opt;
cout<<endl;
if (opt==1){
search(wrd2);
}
if (opt==2){
revr(wrd2,wrd1);
}
if (opt==3){
chcs(wrd2,wrd1);
}
if (opt==4){
re_ch(wrd2,wrd1);
}
if (opt==5){
cout<<"Please change your first word here:";
cin>>wrd1;
cout<<""<<endl;
}
if (opt==6){
cout<<"Please change your second word here:";
cin>>wrd2;
cout<<""<<endl;
}
}
while(opt !=7);
if(opt==7)
cout<<"good bye";
}
Does my code seems valid to you. Again there is no signs of errors on my IDE.
void search(char wrd2[26])
{ bool fnd=false;
char ch;
int x=0, cnt=0;
cout<<"Please enter in a character you would like to search for:";
cin>>ch;
cout<<""<<endl;
fnd=srch(wrd2,ch);
cnt=cntr(wrd2,ch);
x=pos(wrd2,ch);
if(fnd==true)
{
cout<<"Yes! Your character was found! :) ";
if(cnt==1)
{
if(x==0)
{
cout<<ch<<" is the "<<x+1<<"st letter of the word "<<wrd2<<".";
}
if(x==1)
{
cout<<ch<<" is the "<<x+1<<"nd letter of the word "<<wrd2<<".";
}
if(x==2)
{
cout<<ch<<" is the "<<x+1<<"rd letter of the word "<<wrd2<<".";
}
if(x>2)
{
cout<<ch<<" is the "<<x+1<<"th letter of the word "<<wrd2<<".";
}
cout<<" There is "<<cnt<<" letter "<<ch<<" in the word "<<wrd2<<"."<<endl;
cout<<" "<<endl;
}
if(cnt>1)
{
if(x==0)
{
cout<<"It first appears as the "<<x+1<<"st letter of the word. ";
}
if(x==1)
{
cout<<"It first appears as the "<<x+1<<"nd letter of the word. ";
}
if(x==2)
{
cout<<"It first appears as the "<<x+1<<"rd letter of the word. ";
}
if(x>2)
{
cout<<"It first appears as the "<<x+1<<"th letter of the word. ";
}
cout<<"There is "<<cnt<<" "<<ch<<"'s in the word "<<wrd2<<"."<<endl;
cout<<" "<<endl;
}
}
else
{
cout<<"Your character "<<ch<<" was not found in the word "<<wrd2<<". :("<<endl;
cout<<""<<endl;
}
}
bool srch(char wrd[26], char ch)
{
int x=0;
bool fnd=false;
for(x=0; x<=25; x++)
{
if(wrd[x]==ch)
{
fnd=true;
return fnd;
break;
}
}
if(!fnd)
{
return fnd;
}
return 0;
}
int pos(char wrd[26], char ch)
{
for(int x=0;x<26;x++)
{
if (wrd[x] == ch)
{
return x;
}
}
return 0;
}
int cntr(char wrd[26], char ch)
{
int cnt=0;
int x=0;
for(x=0;x<26;x++)
{
if (wrd[x] == ch)
{
cnt++;
}
}
return cnt;
return 0;
}
void revr(char wrd2[26],char wrd1[26])
{
int s,x;
s=strlen(wrd2)-1;
cout<<"Your second word "<<wrd2<<" reversed added to first = ";
cout<<wrd1;
for(x=s;x>=0;x--)
{
cout<<(wrd2[x]);
}
cout<<" "<<endl;
cout<<" "<<endl;
}
void chcs(char wrd2[26], char wrd1[26])
{
char ch;
int s,x;
s=strlen(wrd2)-1;
cout<<"Your second word with case changed proceeded by the first = ";
cout<<wrd1;
for(x=0;x<=s;x++)
{
if(wrd2[x]>= 65 && wrd2[x] <= 90) //is it upper?
ch=wrd2[x]+32;
else if(wrd2[x]>= 97 && wrd2[x] <= 122) //is it lower?
ch=wrd2[x]-32;
else
ch=1; //invlaid char - mark with smiley face
cout<<ch;
}cout<<endl;
}
void re_ch(char wrd2[26], char wrd1[26])
{
char ch;
int s,x;
s=strlen(wrd2)-1;
//cout<<"String with case changed = ";
cout<<"Your second reversed and with the case changed procceeded by the first"<<endl;
cout<<" "<<endl;
cout<<wrd1;
for(x=s;x>=0;x--)
{
if(wrd2[x]>= 65 && wrd2[x] <= 90) //is it upper?
ch=wrd2[x]+32;
else if(wrd2[x]>= 97 && wrd2[x] <= 122) //is it lower?
ch=wrd2[x]-32;
else
ch=1; //invlaid char - mark with smiley face
cout<<ch;
}cout<<" "<<endl;
cout<<" "<<endl;
}
What do i do?
I had a similar problem. Closing and reopening eclipse did it for me.
This is my simple code for a bookshop
There is nothing wrong with the code. I am using DevC++ to run the code and after compling it gives out an error which says 'gets' was not declared in this scope & the same error for puts. Please help me.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<iomanip>
#include<cstring>
using namespace std;
class Book
{
char *title,*author,*publisher,ans;
int price,quant,quant_ent;
public:
Book()
{
title = new char[50];
author = new char[50];
publisher = new char[50];
price = quant = quant_ent = 0;
}
void getdata()
{
cout<<"\nEnter The Title";
gets(title);
cout<<"\nEnter The Author";
gets(author);
cout<<"\nEnter The Publisher";
gets(publisher);
cout<<"\nEnter The Price";
cin>>price;
cout<<"\nEnter The Quantity";
cin>>quant;
}
void display()
{
cout<<setw(15)<<title<<setw(15)<<author<<setw(15)<<publisher<<setw(10)<<quant
<<setw(10)<<price;
}
void search(char search_title[],char search_author[])
{
if(strcmpi(author,search_author)==0)
{
if(strcmpi(title,search_title)==0)
{
cout<<"\nBook Found!";
cout<<"\nEnter The Quantity: ";
cin>>quant_ent;
if(quant_ent <= quant)
{
cout<<"\nThe Title is: ";
puts(title);
cout<<"\nThe Author is: ";
puts(author);
cout<<"\nThe Publisher is: ";
puts(publisher);
cout<<"\nPrice Of Single Copy: "<<price;
cout<<"\nTotal Price = "<<price*quant_ent;
quant = quant - quant_ent;
}
else
{
cout<<"\nSufficient Quantity Not Available!";
}
}
}
}
};
int main()
{
Book obj[10];
int i=0,ch;
char author[50],title[50];
for(;;)
{
cout<<"\n*******MENU********\n1)Enter Details\n2)Buy Book\n3)Display All Books\n4)Exit";
cin>>ch;
switch(ch)
{
case 1:
obj[i].getdata();
i++;
break;
case 2:
cout<<"\nEnter The Authors Name: ";
gets(author);
cout<<"\nEnter The Title: ";
gets(title);
for(int j=0;j<i;j++)
{
obj[j].search(title,author);
}
break;
case 3:
cout<<setw(15)<<"TITLE"<<setw(15)<<"AUTHOR"<<setw(15)<<"PUBLISHER"<<setw(15)<<"QUANTITY"<<setw(15)<<"PRICE";
cout<<"\n"<<setw(75)<<"-----------------------------------------------------------------------------------------------------";
for(int j=0;j<i;j++)
{
cout<<"\n";
obj[j].display();
}
case 4:
exit(1);
};
}
}
Because it's declared in stdio.h (cstdio in C++) header and you haven't included it.
But you shall not use gets. It's a hopelessly broken function. Use fgets instead. Even better, ditch the naked pointers to char arrays and use std::string class instead.
i've been thinking how to do this "save to file" thing all night, but it seems that luck wasnt on my side.. .
I am new to this, and i want to get learned, thats all.
I am from Bulgaria, and for those who wonder what is "edinen", this is your citizen number as a member of that country. (I still dont know how to explain this...)
Here is what I've got
(I've already included the "fstream", but i still dont know how to use it!
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define n 30
int num=0;
struct uslugi
{
char name[30];
char surname[30];
char lastname[30];
char illness[30];
long int edinen;
}grupa[n];
void add_record();
void show_record();
void search_record();
void remove_record();
void add_record() // FUNCTION - ADD RECORD(S)
{
system("title Add Record");
int br;
cout<<"\n How many pacients do you want to enter?";
cout<<"\n >> ";
cin>>br;
for(int i=num;i<num+br;i++)
{
cout<<"\n \t\t ENTERING DATA FOR PACIENT NUMBER - "<<i+1<<endl;
cout<<"\n Name:";
cout<<"\n >> ";
cin>>grupa[i].Name;
cout<<"\n Surname:";
cout<<"\n >> ";
cin>>grupa[i].surname;
cout<<"\n Lastname:";
cout<<"\n >> ";
cin>>grupa[i].lastname;
cout<<"\n edinen:";
cout<<"\n >> ";
cin>>grupa[i].edinen;
cout<<"\n Ill from:";
cout<<"\n >> ";
cin>>grupa[i].illness;
}
num=num+br;
}
void show_record() // FUNCTION - SHOW RECORD(S)
{
if (num==0)
{
cout<<"\t\t by far there are no pacient at all \n";
}
cout<<"\n \t\t\t list with all pacients"<<num<<endl;
for(int i=0;i<num;i++)
{
cout<<"\n Name:";
cout<<"\n >> "<<grupa[i].name;
cout<<"\n Surname:";
cout<<"\n >> "<<grupa[i].surname;
cout<<"\n Lastname:";
cout<<"\n >> "<<grupa[i].lastname;
cout<<"\n edinen:";
cout<<"\n >> "<<grupa[i].edinen;
cout<<"\n Ill from:";
cout<<"\n >> "<<grupa[i].illness;
cout<<"\n\n";
}
}
void remove_record() // FUNCTION - DELETE RECORD(S)
{
int k,index;
for(int i=0;i<num;i++)
{
cout<<"\n("<<i+1<<") "<<endl;
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
}
cout<<"Who do you want to delete? \n >> ";
cin>>index;
for(k=0;k<num;k++)
{
if(k>=index)
{
grupa[k].edinen=grupa[k+1].edinen;
}
if(k==num-1)
break;
else
cout<<"BY FAR THERE IS NO DATA HERE"<<endl;
}
num--;
for (i=0;i<num;i++)
{
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
}
}
void print_count() // FUNCTION - PRINTING
{
cout<<"\n \t\t\t NUMBER OF PACIENTS - "<<num<<endl;
cout<<""<<endl;
}
void search_record() // FUNCTION - SEARCHING
{
int flag=0;
long int tempegn;
cout<<"\n Type the edinen for the pacient you search: ";
cout<<"\n >> ";
cin>>tempegn;
for (int i=0;i<n;i++)
if(tempegn==grupa[i].edinen)
{
cout<<"Pacient name: "<<grupa[i].name<<endl;
cout<<"Surname: "<<grupa[i].surname<<endl;
cout<<"Lastname: "<<grupa[i].lastname<<endl;
cout<<"edinen: "<<grupa[i].edinen<<endl;
cout<<"Ill from: "<<grupa[i].illness<<endl<<endl;
flag++;
}
if (!flag)
{
cout<<"\n\t PACIENT WITH THAT NAME DOESNT EXCIST \n\n";
}
}
void main() // MAIN FUNCTION (MENU)
{
int choice;
do
{
cout<<"\n\t\t******************* Menu *******************"<<endl;
cout<<"\t\t* *";
cout<<"\n\t\t* 1.Add new pacient *";
cout<<"\n\t\t* 2.Search for a pacient by edinen *";
cout<<"\n\t\t* 3.Delete pacient *";
cout<<"\n\t\t* 4.List with all pacients *";
cout<<"\n\t\t* 5.Exit *";
cout<<"\n\t\t* *\n";
cout<<"\t\t************************************************"<<endl;
cout<<"\n Type your choice! ";
cout<<"\n >> ";
cin>>choice;
switch(choice)
{
case 1:{print_count();add_record();break;}
case 2:{print_count();search_record();break;}
case 3:{print_count();remove_record();break;}
case 4:{print_count();show_record();}
}
}
while(choice!=5);
}
I would suggest you read a book, or at least some documentation about this. Also, to understand how this kind of input works, it is required knowledge of oop concepts. As far as I noticed you do not have such a knowledge, I would suggest you stick for a while with procedural i/o functions from C, like fprintf. A google search will show many resources about them and they are pretty easy to understand, at least compared with those from C++, like the ones you are using.
To answer to your question, I will try to explain how to achive what you want. Note that this is a very simplified explanation and not 100% accurate.
Firstly, you need to open the file and create a i/o stream, using ifstream for input files or ofstrsam for output files:
ifstream f("input_filename");
ofstream g("output_filename);
After you created f and/or g (they are not both requied. Also, f and g are just random names) you can use them in the same way you use cin and cout, in this case, as f is for input, it should be used like cin.