Creating and editing records
Jun 10, 2012 at 9:45am UTC
I've been working on a little project, but I can't seem to get around one problem. I want my program to replace the first pair of '0' in the record to a new pair I will indicate in the program. I have no problems loading the record from the text file, but the program tries to replace the record something goes wrong and I'm not sure what I'm doing wrong. Not even sure if this is possible actually but any input will be greatly appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;
char filename[20];
int r=0;
int input[2];
int data[31][6];
int create()
{
char C;
enter:
cout<<"Create new monthly record" <<endl;
cout<<"Enter Month(mmyyyy.txt):" ;
cin>>filename;
ofstream monthly_record (filename);
if (monthly_record.is_open())
{
for (int x=0;x<=31;x++)
{
for (int y=0;y<=5;y++)
{
data[x][y]=0;
cout<<data[x][y]<<" " ;
monthly_record<<data[x][y];
monthly_record<<" " ;
y++;
data[x][y]=0;
cout<<data[x][y]<<" || " ;
monthly_record<<data[x][y];
monthly_record<<" " ;
}
cout<<endl;
monthly_record<<endl;
}
monthly_record.close();
cout<<"Action successful" ;
r=2;
getch();
return (r);
}
else
cout<<"Action cannot be completed!" <<endl;
retry:
cout<<"Do you wish to continue?(y/n): " ;
cin>>C;
switch (C)
{
case 'y' :
case 'Y' : goto enter;
break ;
case 'n' :
case 'N' : r=1;
return (r);
break ;
default : goto retry;
break ;
}
}
int recording()
{
cout<<"Recording results" <<endl;
cout<<"Enter month(mmyyyy.txt):" ;
cin>>filename;
cout<<"Enter first result: " ;
cin>>input[0];
cout<<"Enter second result: " ;
cin>>input[1];
fstream access_record;
access_record.open(filename);
if (access_record.is_open())
{
for (int a=0;a<=31;a++)
{
for (int b=0;b<=5;b++)
{
access_record>>data[a][b];
cout<<data[a][b]<<" " ;
b++;
access_record>>data[a][b];
cout<<data[a][b]<<" || " ;
}
cout<<endl;
}
for (int x=0;x<=31;x++)
{
for (int y=0;y<=5;y=y+2)
{
if ((data[x][y]==0)&&(data[x][++y]==0))
{
data[x][y]=input[0];
data[x][++y]=input[1];
break ;
}
else
cout<<"." ;
}
}
for (int x=0;x<=31;x++)
{
for (int y=0;y<=5;y++)
{
cout<<data[x][y]<<" " ;
access_record<<data[x][y];
access_record<<" " ;
y++;
cout<<data[x][y]<<" || " ;
access_record<<data[x][y];
access_record<<" " ;
}
cout<<endl;
access_record<<endl;
}
access_record.close();
cout<<"Action Complete!" ;
r=2;
getch();
return (r);
}
else
cout<<"File cannot be opened!" ;
r=1;
getch();
return (r);
}
int main()
{
system("cls" );
cout<<"Last action: " ;
if (r==0)
cout<<"None!" <<endl<<endl;
else if (r==1)
cout<<"Not completed!" <<endl<<endl;
else
cout<<"Completed!" <<endl<<endl;
cout<<"Available action: \n[A]Create new monthly record\n[B]Enter new draw record\n[X]Exit\n" ;
cout<<"Enter desired action!: " ;
char A;
cin>>A;
switch (A)
{
default : main();
break ;
case 'a' :
case 'A' : create();
main();
break ;
case 'b' :
case 'B' : recording();
main();
break ;
case 'x' :
case 'X' : return 0;
break ;
}
}
I know it's a mess, please bear with me. :)
Jun 10, 2012 at 12:03pm UTC
data[a][b]
This is an error when a == 32
. Either change make the array larger or change the loop condition so that a < 32
always.
Jun 14, 2012 at 9:22am UTC
thank you for pointing that out.
Last edited on Jun 15, 2012 at 3:14am UTC
Topic archived. No new replies allowed.