Suggestions about my program

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

int clrg()
{
Sleep(10);
while(_kbhit()) _getch();
return 0;
}

int chkq(int ck, string ca)
{
system("CLS");
clrg();
char ch;
int x;
x = 0;
while(x == 0)
{
system("CLS");
cout<< "Are you sure you want to pick "<< ca<< "?"<< endl;
cout<< endl;
cout<< " 1- YES"<< endl;
cout<< " 2- NO"<< endl;
ch = _getch();
if((ch == '1') || (ch == VK_NUMPAD1))
{
ck = 0;
return ck;
}
if ((ch == '2') || (ch == VK_NUMPAD2))
{
ck = 1;
return ck;
}
}
}

int svebk()
{
system("CLS");
vector<string> results;
string resname, txt;
int y, cnt;
cnt = 0;
y = 0;
cout<< "Name your results: ";
resname = "";
while(resname == "")
{
getline(cin, resname);
}
ofstream s;
s.open("RESULTS.txt", ios::app);
s<< "____________________________________________________________"<< endl;
s<< " "<< resname<< endl;
ifstream res;
res.open("resl.txt", ios::in);
if(res.is_open())
{
while(!res.eof())
{
y++;
getline(res, txt);
results.push_back(txt);
s<< txt<< endl;
}
cnt = y;
}
res.close();
s.close();
ofstream rewrite;
rewrite.open("resl.txt", ios::out);
rewrite<< "saved"<< endl;
y = -1;
for(int x = 1; x <= cnt; x++)
{
y++;
txt = results[y];
rewrite<< txt<< endl;
}
rewrite.close();
system("PAUSE");
system("CLS");
return 0;
}

int shwvte(string cand)
{
system("CLS");
cout<< " YOU HAVE CHOSEN"<< endl;
cout<< ""<< endl;
cout<< ""<< endl;
cout<< ""<< endl;
cout<< " "<< cand<< endl;
Sleep(1500);
while(_kbhit()) _getch();
system("CLS");
return 0;
}

int vte()
{
system("CLS");
vector<string> canddts;
vector<int> write;
vector<string> isdat;
char ch;
string cand;
int y, count, y1, lp, res, t, chk1, chkqu;
int candi1, candi2, candi3, candi4, candi5, candi6, candi7, candi8, candi9;
candi1 = 0;
candi2 = 0;
candi3 = 0;
candi4 = 0;
candi5 = 0;
candi6 = 0;
candi7 = 0;
candi8 = 0;
candi9 = 0;
chk1 = 0;
chkqu = 0;
y1 = 0;
lp = 0;
t = 0;
ifstream chk;
chk.open("candidates.txt", ios::in);
if(!(chk.is_open()))
{
cout<< "SELECT YOUR CANDIDATES BEFORE VOTING!"<< endl;
cout<< endl;
system("PAUSE");
system("CLS");
return 0;
}
if(chk.is_open())
{
while(!chk.eof())
{
chk1++;
if(chk1 == 2)
{
chk.seekg(ios::beg);
}
getline(chk, cand);
isdat.push_back(cand);
}
if (isdat[0] == "")
{
system("CLS");
cout<< "You haven't selected candidates yet!"<< endl;
cout<< endl;
cout<< endl;
system("PAUSE");
system("CLS");
chk.close();
return 0;
}
chk.close();
}
while (lp == 0)
{
system("CLS");
Sleep(5);
while(_kbhit()) _getch();
//SETS UP THE MENU------------------------------------------------------------------------
Last edited on
cout<< " PRESS THE NUMBER OF YOUR CHOICE TO VOTE"<< endl;
//counting the candidates
ifstream cans;
cans.open("candidates.txt", ios::in);
y = 0;
while(!cans.eof())
{
getline(cans, cand);
y++;
}
count = y;
cans.close();
//now we will get each line as a vector assuming each candidate is written
ifstream ci;
ci.open("candidates.txt", ios::in);
y = 0;
while(!ci.eof())
{
y++;
if ((y >= 2) && (cand == ""))
{
system("CLS");
cout<< "You need to have candidates to vote for!"<< endl;
cout<< endl;
cout<< endl;
system("PAUSE");
system("CLS");
return 0;
}
cand = "";
getline(ci, cand);
if (cand > "")
{
canddts.push_back(cand);
}
}
ci.close();
y = -1;
y1 = 0;
for (int x = 2; x <= count; x++)
{
y1++;
y++;
cand = canddts[y];
cout<< " "<< y1<< "- "<< cand<< endl;
}
cout<< endl;
cout<< "Total votes so far: "<< t<< endl;
cout<< endl;
cout<< endl;
cout<< " Programed by: Jonathan Whitlock"<< endl;
//END OF THE MENU SECTION----------------------------------------------------------------
ch = _getch();
ch = toupper(ch);
//we want the user to press a button now
if(((ch == '1') || (ch == VK_NUMPAD1)) && (count > 1))
{
cand = canddts[0];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi1++;
continue;
}
if (((ch == '2') || (ch == VK_NUMPAD2)) && (count > 2))
{
cand = canddts[1];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi2++;
continue;
}
if (((ch == '3') || (ch == VK_NUMPAD3)) && (count > 3))
{
cand = canddts[2];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi3++;
continue;
}
if (((ch == '4') || (ch == VK_NUMPAD4)) && (count > 4))
{
cand = canddts[3];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi4++;
continue;
}
if (((ch == '5') || (ch == VK_NUMPAD5)) && (count > 5))
{
cand = canddts[4];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi5++;
continue;
}
if (((ch == '6') || (ch == VK_NUMPAD6)) && (count > 6))
{
cand = canddts[5];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi6++;
continue;
}
if (((ch == '7') || (ch == VK_NUMPAD7)) && (count > 7))
{
cand = canddts[6];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi7++;
continue;
}
if (((ch == '8') || (ch == VK_NUMPAD8)) && (count > 8))
{
cand = canddts[7];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi8++;
continue;
}
if (((ch == '9') || (ch == VK_NUMPAD9)) && (count > 9))
{
cand = canddts[8];
chkqu = chkq(chkqu, cand);
if (chkqu == 1)
{
clrg();
continue;
}
shwvte(cand);
t++;
candi9++;
continue;
}
if ((GetAsyncKeyState(VK_MENU)) && (GetAsyncKeyState(VK_BACK)))
{
y = 0;
for(int x = 1; x <= 9; x++) //defines the number of "slots" available in the vector
{
y++;
write.push_back(0);
}
//writes the results to a vector so we can exclude results of zero
write[0] = candi1;
write[1] = candi2;
write[2] = candi3;
write[3] = candi4;
write[4] = candi5;
write[5] = candi6;
write[6] = candi7;
write[7] = candi8;
write[8] = candi9;
//we are saving the amount of results to a file
ofstream sve;
sve.open("votes.txt", ios::out);
y = -1;
for(int x = 1; x < count; x++)
{
y++;
res = write[y];
sve<< res<< endl;
}
sve.close();
y = 0;
y1 = -1;
ofstream r;
r.open("resl.txt",ios::out);
r<< "__________________________________________________________"<< endl;
r<< " RESULTS"<< endl;
r<< endl;
for(int x = 1; x < count; x++)
{
y++;
y1++;
cand = canddts[y1];
res = write[y1];
r<< y<< "- "<< cand<< ": "<< res<< endl;
}
r<< "________________________________"<< endl;
r<< endl;
r<< "Total votes: "<< t<< endl;
r.close();
system("CLS");
return 0;
}
}
}

int namecand()
{
Sleep(5);
while(_kbhit()) _getch();
vector<string> candi;
string txt;
system("CLS");
int numcans, y, c;
string cand, v;
system("CLS");
cout<< "How many candidates? ";
c = -1;
y = 0;
while((c == -1) || (c > 9)) // makes sure the user doesnt input an invalid number
{
cin>> c;
}
cout<< endl;
for(int x = 1; x <= c; x++) // gets user input from the first to the last candidate
{
system("CLS");
y++; // allows to call candidates by a number
cand = "";
cout<< "Name of Candidate "<< y<< ": ";
while(cand == "")
{
getline(cin, cand); // makes sure that the program actually gets the line
}
candi.push_back(cand);
}
ofstream can;
can.open("candidates.txt", ios::out);
y = -1;
for(int ex = 1; ex <= c; ex++)
{
y++;
v = candi[y];
can<< v<< endl;
}
can.close();
system("CLS");
cout<< "Candidates successfully added!"<< endl;
cout<< "press ENTER to continue"<< endl;
cin.get();
system("CLS");
return 0;
}

int shw()
{
system("CLS");
string txt;
int q, y;
y = 0;
q = 0;
ifstream i;
i.open("resl.txt", ios::in);
if (i.is_open())
{
q = 1;
}
if (q == 1)
{
while(!i.eof())
{
y++;
getline(i, txt);
if ((txt == "") && (y == 1))
{
system("CLS");
q = 0;
break;
}
cout<< txt<< endl;
}
i.close();
}
if (q == 0)
{
return 0;
}
cout<< endl;
cout<< endl;
system("PAUSE");
system("CLS");
return 0;
}

int sve()
{
system("CLS");
vector<string> results;
string resname, txt;
int y, cnt;
cnt = 0;
y = 0;
cout<< "Name your results: ";
resname = "";
while(resname == "")
{
getline(cin, resname);
}
ofstream s;
s.open("RESULTS.txt", ios::app);
s<< "____________________________________________________________"<< endl;
s<< " "<< resname<< endl;
ifstream res;
res.open("resl.txt", ios::in);
if(res.is_open())
{
while(!res.eof())
{
y++;
getline(res, txt);
results.push_back(txt);
s<< txt<< endl;
}
cnt = y;
}
res.close();
s.close();
ofstream rewrite;
rewrite.open("resl.txt", ios::out);
rewrite<< "saved"<< endl;
y = -1;
for(int x = 1; x <= cnt; x++)
{
y++;
txt = results[y];
rewrite<< txt<< endl;
}
rewrite.close();
system("CLS");
cout<< "Current results successfully saved!"<< endl;
cout<< endl;
cout<< endl;
cout<< endl;
system("PAUSE");
system("CLS");
return 0;
}

int chksave()
{
int i, x, save;
char ch;
save = 0;
i = 0;
x = 0;
string txt;
ifstream s;
s.open("resl.txt", ios::in);
if(s.is_open())
{
i = 1;
}
if (i == 0)
{
system("CLS");
return 0;
}
if (i == 1)
{
getline(s, txt);
if ((txt == "saved") || (txt == ""))
{
save = 1;
}
}
s.close();
if (save == 0)
{
system("CLS");
clrg();
while(x == 0)
{
system("CLS");
cout<< "Opening another voting session will delete your previous one, would you like to save?"<< endl;
cout<< endl;
cout<< endl;
cout<< " 1- YES"<< endl;
cout<< " 2- NO"<< endl;
ch = _getch();
if((ch == '1') || (ch == VK_NUMPAD1))
{
clrg();
svebk();
clrg();
system("CLS");
return 0;
}
if ((ch == '2') || (ch == VK_NUMPAD2))
{
system("CLS");
return 0;
}
}
}
}

int opnsve()
{
system("CLS");
string txt;
int i, y, c;
c = 0;
y = 0;
i = 0;
ifstream o;
o.open("RESULTS.txt", ios::in);
if (o.is_open())
{
i = 1;
}
o.close();
if (i == 1)
{
ifstream st;
st.open("RESULTS.txt", ios::in);
while(!st.eof())
{
c++;
txt = "";
getline(st, txt);
if (txt == "")
{
y++;
}
cout<< txt<< endl;
if ((y == 1) && (y == c))
{
system("CLS");
return 0;
}
}
st.close();
cout<< endl;
cout<< endl;
system("PAUSE");
}
if (i == 0)
{
system("CLS");
cout<< "There is nothing saved!"<< endl;
cout<< endl;
cout<< endl;
system("PAUSE");
}
system("CLS");
return 0;
}

int clearsve()
{
system("CLS");
Sleep(5);
while(_kbhit()) _getch();
char ch;
int cand, res, result, vts, x;
x = 0;
vts = 0;
cand = 0;
res = 0;
result = 0;
while(x == 0)
{
system("CLS");
cout<< "Are you absolutely sure you want to delete ALL DATA?"<< endl;
cout<< endl;
cout<< " 1- YES"<< endl;
cout<< " 2- NO"<< endl;
ch = _getch();
ch = toupper(ch);
if ((ch == '1') || (ch == VK_NUMPAD1))
{
ifstream cans;
cans.open("candidates.txt", ios::in);
if(cans.is_open())
{
cand = 1;
}
cans.close();
ifstream r;
r.open("resl.txt", ios::in);
if(r.is_open())
{
res = 1;
}
r.close();
ifstream re;
re.open("RESULTS.txt", ios::in);
if(re.is_open())
{
result = 1;
}
re.close();
if (cand == 1)
{
ofstream c;
c.open("candidates.txt", ios::out);
c<< "";
c.close();
}
if (res == 1)
{
ofstream rx;
rx.open("resl.txt", ios::out);
rx<< "";
rx.close();
}
if (result == 1)
{
ofstream rrs;
rrs.open("RESULTS.txt", ios::out);
rrs<< "";
rrs.close();
}
ifstream v;
v.open("votes.txt", ios::in);
if(v.is_open())
{
vts = 1;
v.close();
}
if (vts = 1)
{
ofstream vt;
vt.open("votes.txt", ios::out);
vt<< "";
vt.close();
}
system("CLS");
cout<< endl;
cout<< endl;
cout<< endl;
cout<< endl;
cout<< " ALL DATA HAS BEEN CLEARED!"<< endl;
cout<< endl;
cout<< endl;
cout<< endl;
system("PAUSE");
system("CLS");
return 0;
}
if ((ch == '2') || (ch == VK_NUMPAD2))
{
system("CLS");
return 0;
}
}
}

int main()
{
int x, no, num;
char ch; //Character input for menu
x = 0;
bool t = false;
while(x == 0)
{
system("CLS");
ch = '0';
cout<< " MENU"<< endl;
cout<< ""<< endl;
cout<< " 1- Start voting"<< endl;
cout<< " 2- Select candidates"<< endl;
cout<< " 3- Show results"<< endl;
cout<< " 4- Save results to file"<< endl;
cout<< " 5- Open saved results"<< endl;
cout<< " 6- Clear all memory and saved data"<< endl;
cout<< " 7- Exit program"<< endl;
ch = _getch();
if ((ch == '1') | (ch == VK_NUMPAD1))
{
chksave();
vte();
Sleep(5);
while(_kbhit()) _getch();
}
if ((ch == '2') | (ch == VK_NUMPAD2))
{
Sleep(5);
while(_kbhit()) _getch();
namecand();
}
if ((ch == '3') | (ch == VK_NUMPAD3))
{
shw();
Sleep(5);
while(_kbhit()) _getch();
}
if ((ch == '4') | (ch == VK_NUMPAD4))
{
sve();
Sleep(5);
while(_kbhit()) _getch();
}
if ((ch == '5') || (ch == VK_NUMPAD5))
{
opnsve();
Sleep(5);
while(_kbhit()) _getch();
}
if ((ch == '6') | (ch == VK_NUMPAD6))
{
clearsve();
}

if ((ch == '7') | (ch == VK_NUMPAD7))
{
return 0;
}
}
}
closed account (zb0S216C)
And your question is? You've posted a whole lot of poorly formatted code with without indicating what we're looking for. Kinda' like a blind man in the Amazon.

Wazzak
My government teacher is holding a ballet and so i offered to write this program for him. We will be recieving 1000+ votes, so you can imagine his previous ways of doing this (tallying the votes and counting them out) were a bit strenuous. As a beginner, i decided to post this query: are there any suggestions you have that would make this program better than it is? If so, how? Would you mind explaining it for me (and/or giving me an example)? Thank you for your time, i look forward to your submissions!
Last edited on
I'm nominating this as the single worst thread in the history of the forum.
I just want your suggestions on improvements i can make...

also:

@helios
If i wanted your criticism, I would have asked for it. Insulting a beginner is like insulting an animal because it doesn't understand what you're saying. I've not exactly "mastered" C++, if you haven't realized it yet. So, please discard your OPINION of my post and answer my question, or please refrain from posting anything at all. My understanding is that this thread is completely valid in this forum section, so please tell me if it is not.
Last edited on
First, when posting you should make sure you put code tags around your code. Just skimming through your code (there's too much for me to bother reading through) I notticed you have some really odly named functions like clrg, chkq and shwvte. They should be named something more descriptive.
My understanding is that this thread is completely valid in this forum section, so please tell me if it is not.
Okay.
1. Don't post post thousands of lines of code without any formatting. I can't believe this even needs to be said.
2. Don't post thousands of lines of code without some sort of introduction.
3. It's unreasonable to ask for suggestions about such a large amount of code. No one will read through the whole thing. Your naming conventions don't make it any easier or compelling, either.
Im sorry that i'm a beginner, but at least i'm admitting that. Also, next time i do this, i will post an intro before the code. I hope your next post has somthing usful to offer to me... your criticism isn't doing anything to accelarate my programming skills.
Topic archived. No new replies allowed.