#include<fstream>
#include<iostream>
#include<string>

using namespace std;

int main ()
{
struct Candidate{
char RegNomer[4];
char name[60];
char party[60];
char age[3];
char xp[3];
long int votes;
}candidate[200];

int i=-1;
int i1=0;
int check=0;
char choice[1];

while (true)
{
system ("cls");

cout<<"/============\\=/============\\=/============\\=/============\\=/============\\ "<<"\n";
cout<<"| save | | create a | | banned | | scoreboard | | ties | "<<"\n";
cout<<"| | | candidate | | candidates | | | | | "<<"\n";
cout<<"| 1 | | 2 | | 3 | | 4 | | 5 | "<<"\n";
cout<<"\\============/ \\============/ \\============/ \\============/ \\============/ "<<"\n";

do{
cout<<"|Please choose an option:";
cin>>choice;
}while ((atoi(choice)<0)||(atoi(choice)>5)); // i have only 5 optsions

if ((atoi(choice)==1))
{
}

if ((atoi(choice)==2)) 
{

i++; cin.clear();cin.sync();
do{
cout<<"|Enter the candidate's Registration Number:";
cin>>candidate[i].RegNomer;
}while ((candidate[i].RegNomer[0]<'a')||(candidate[i].RegNomer[0]>'z')||(candidate[i].RegNomer[1]<'0')||(candidate[i].RegNomer[1]>'9')||(candidate[i].RegNomer[2]<'0')||(candidate[i].RegNomer[2]>'9')||(candidate[i].RegNomer[3]<'0')||(candidate[i].RegNomer[3]>'9')); // the RegNomer consist of a small letter + 3 digits for instance: s123
cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;

check=0; cin.clear();cin.sync();

cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;
cout<<"candidate[check].RegNomer="<<candidate[check].RegNomer<<endl;
while (check<i-1)
{ 
if ((candidate[check].RegNomer[0]==candidate[i].RegNomer[0])&&(candidate[check].RegNomer[1]==candidate[i].RegNomer[1])&&(candidate[check].RegNomer[2]==candidate[i].RegNomer[2])&&(candidate[check].RegNomer[3]==candidate[i].RegNomer[3]))
{cout<<"If-a se izpulni\n";
cout<<"|Another candidate is already using this Registration Number.\n";
do{
cout<<"|Enter the candidate's Registration Number:";
cin>>candidate[i].RegNomer;
cin.clear();cin.sync();
}while (((candidate[i].RegNomer[0]<'a')||(candidate[i].RegNomer[0]>'z')||(candidate[i].RegNomer[1]<'0')||(candidate[i].RegNomer[1]>'9')||(candidate[i].RegNomer[2]<'0')||(candidate[i].RegNomer[2]>'9')||(candidate[i].RegNomer[3]<'0')||(candidate[i].RegNomer[3]>'9'))&&((candidate[check].RegNomer[0]==candidate[i].RegNomer[0])&&(candidate[check].RegNomer[1]==candidate[i].RegNomer[1])&&(candidate[check].RegNomer[2]==candidate[i].RegNomer[2])&&(candidate[check].RegNomer[3]==candidate[i].RegNomer[3])));
} //here i check if the candidate's number is not already taken.

check++;
}
cin.clear();cin.sync();

cout<<"|Enter the candidate's name:";
cin.getline(candidate[i].name,60);
cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;
cout<<"candidate[check].RegNomer="<<candidate[check].RegNomer<<endl;
cout<<"candidate[i].name="<<candidate[i].name<<endl;
cout<<"candidate[check].name="<<candidate[i].name<<endl;
cin.clear();cin.sync();
cout<<"|Enter the candidate's political party:";
cin.getline(candidate[i].party,60);
cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;
cout<<"candidate[check].RegNomer="<<candidate[check].RegNomer<<endl;
cout<<"candidate[i].name="<<candidate[i].name<<endl;
cout<<"candidate[check].name="<<candidate[i].name<<endl; 
cout<<"candidate[i].party="<<candidate[i].party<<endl;
cout<<"candidate[check].party="<<candidate[check].party<<endl;
cin.clear();cin.sync();
do{
cout<<"|Enter the age of the candidate:";
cin>>candidate[i].age;
}while((atoi(candidate[i].age)<1)||(atoi(candidate[i].age)>200));

cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;
cout<<"candidate[check].RegNomer="<<candidate[check].RegNomer<<endl;
cout<<"candidate[i].name="<<candidate[i].name<<endl;
cout<<"candidate[check].name="<<candidate[i].name<<endl; 
cout<<"candidate[i].party="<<candidate[i].party<<endl;
cout<<"candidate[check].party="<<candidate[check].party<<endl;
cout<<"candidate[i].age="<<candidate[i].age<<endl;
cout<<"candidate[check].age="<<candidate[i].age<<endl; 
cin.clear();cin.sync();
do{
cout<<"|Enter the ears of experience in politics:";
cin>>candidate[i].xp;
}while((atoi(candidate[i].xp)<0)||(atoi(candidate[i].xp)>=atoi(candidate[i].age)));

cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;
cout<<"candidate[check].RegNomer="<<candidate[check].RegNomer<<endl;
cout<<"candidate[i].name="<<candidate[i].name<<endl;
cout<<"candidate[check].name="<<candidate[i].name<<endl; 
cout<<"candidate[i].party="<<candidate[i].party<<endl;
cout<<"candidate[check].party="<<candidate[check].party<<endl;
cout<<"candidate[i].age="<<candidate[i].age<<endl;
cout<<"candidate[check].age="<<candidate[i].age<<endl; 
cout<<"candidate[i].xp="<<candidate[i].xp<<endl;
cout<<"candidate[check].xp="<<candidate[check].xp<<endl;
cin.clear();cin.sync();
cout<<"|Enter the number of votes the candidate has:";
cin>>candidate[i].votes;
cout<<"candidate[i].RegNomer="<<candidate[i].RegNomer<<endl;
cout<<"candidate[check].RegNomer="<<candidate[check].RegNomer<<endl;
cout<<"candidate[i].name="<<candidate[i].name<<endl;
cout<<"candidate[check].name="<<candidate[i].name<<endl; 
cout<<"candidate[i].party="<<candidate[i].party<<endl;
cout<<"candidate[check].party="<<candidate[check].party<<endl;
cout<<"candidate[i].age="<<candidate[i].age<<endl;
cout<<"candidate[check].age="<<candidate[i].age<<endl; 
cout<<"candidate[i].xp="<<candidate[i].xp<<endl;
cout<<"candidate[check].xp="<<candidate[check].xp<<endl;
cout<<"candidate[i].votes="<<candidate[i].votes<<endl;
cout<<"candidate[check].votes="<<candidate[i].votes<<endl; 
cin.clear();cin.sync();
}
system ("PAUSE"); 
}
return 0; 
}

Hi guys,
Well basically I have a weird error that simply blows my mind away.I've asked friends but when even my IT teacher (i'm a student in high school) couldn't find the reason for this bug I decided to go and ask the other 3 billion people using the internet.Basically after every second entry of a string a concatenation is activated or something...i'm not even sure exactly how to explain it so here's my code.

This is just the part of the program where the entry is done.
All the extra couts i've put are there to track the progression of which the values of different the strings change for no apparent reason.Thank You all for you help!

P.S. also on a side note can any1 tell me how to delete intervals in a string ? I've thought of a way how with a cycle but i was wondering if there was a function or something.The think i'm aiming for is this:
From: 1 34 232 3 4
To get: 13423234

Thank You all again so much!

One error is that the character arrays xp and choice are not big enough -- you need to leave room for the string's null terminator. atoi() could return wrong values when the string does not contain a null terminator byte.

Since you include <string> header file you might as well use std::string instead of those character arrays so that you don't have to guess about how big to make character arrays.

Another suggestion is to convert coice and xp to integers before making those if statements so that atoi() is clled only once instead of dozdens of times. That will make your program run a little faster too.

P.S. also on a side note can any1 tell me how to delete intervals in a string

Use a loop and check each character for white space using isspace() macro found in ctype.h. If you use std::string instead of character arrays you can call it's erase() method which will delete the character. Otherwise, if you use character arrays you have to shift all bytes from point of space left one byte. One way to do that, and its also the most efficient way, is to call memmove().

That's all fine and dandy but it does NOT explain the fact that my strings change their values for no apparent reason.PLEASE RUN THE CODE TO TEST IT OUT FOR YOURSELVES. It does nothing more than just entering values for these elements.

That's all fine and dandy but it does NOT explain the fact that my strings change their values for no apparent reason.

Yes, it does. Let's do a little visualization:

 a   b
---------
| | | | |
---------

Copy "12\0" to a (note how a isn't big enough, so it overflows into b):

 a   b
---------
|1|2|0| |
---------

Copy "x\0" to b (note how the \0 from a is overwritten):

 a   b
---------
|1|2|x|0|
---------

Print a, and the output is everything up to \0: "12x"

You're a victim of buffer overflow.

HOLY SHIT YOU WERE RIGHT...i just made the strings bigger xp[60] , age[60] ect. and it was fixed.Thank you o'great one ! (bow)

why is it when i change:

do{
cout<<"|Enter the candidate's Registration Number:";
cin>>candidate[i].RegNomer;
}while ((candidate[i].RegNomer[0]<'a')||(candidate[i].RegNomer[0]>'z')||(candidate[i].RegNomer[1]<'0')||(candidate[i].RegNomer[1]>'9')||(candidate[i].RegNomer[2]<'0')||(candidate[i].RegNomer[2]>'9')||(candidate[i].RegNomer[3]<'0')||(candidate[i].RegNomer[3]>'9')); 

to

do{
cout<<"|Enter the candidate's Registration Number:";
cin.getline(candidate[i].RegNomer,4);
}while ((candidate[i].RegNomer[0]<'a')||(candidate[i].RegNomer[0]>'z')||(candidate[i].RegNomer[1]<'0')||(candidate[i].RegNomer[1]>'9')||(candidate[i].RegNomer[2]<'0')||(candidate[i].RegNomer[2]>'9')||(candidate[i].RegNomer[3]<'0')||(candidate[i].RegNomer[3]>'9')); 

the cycle doesn't seem to stop? o.O

NEVERMIND ...i forgot the \0 again at the end of the string...silly me...cin.getline(candidate[i].RegNomer,5) is the right way

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.