Banner

Sponsor

Login


Welcome Back!
Guest
Guest

Register

Lost your password?

41 users online



while(1) and Segfaults

while(1) and Segfaults

Currently viewing this thread: 1 (0 members and 1 guests)


NoT_SupeRmaN

NoT_SupeRmaN

Status: Offline!

while(1) and Segfaults

Alright, so i just finished my program, which is a homework organizer. I wrapped it with while(1){ and }, so that i didnt have to type the filename every time i wanted to execute a menu option. My code is below: The problem is that every 3 or so menu options that occur, I get a segfault and the program shuts off. It's not really a problem, since this is only a personal program. I was just wondering if there was a way to prevent the segfaults.

code:

Code:


#include <stdio.h>
#include <mysql/mysql.h>
#include <iostream.h>

MYSQL *pConnection;
MYSQL *cConnection;
char subject[255];
int number;
char assignment[255];
char Query[255];
int numberdone;

int main()
{ // begin main
pConnection = mysql_init(NULL);
MYSQL_RES *pResult; //pointer to the result set structure
MYSQL_RES *cResult;
MYSQL_ROW cRow;
MYSQL_ROW Row; //row information
int action;
while(1)
{
cout << "(1) View Undone Homeworks" << endl;
cout << "(2) Add A Homework" << endl;
cout << "(3) View All Homeworks" << endl;
cout << "(4) Mark a Homework as Done" << endl;
cout << "(5) View Finished" << endl;
cout << "(6) Exit" << endl;
cin >> action;
//START MYSQL CONNECTION
if(action == 1)
{
printf("-------------Undone Homeworks------------\r\n");
mysql_real_connect(pConnection,"localhost","root","********8","homework",0,NULL,0);
mysql_query(pConnection,"SELECT * FROM homework where status = 'n'"); //query the database
pResult = mysql_store_result(pConnection);
printf("ID | Subject | Number | Assignment | Done?\r\n");
while ((Row = mysql_fetch_row(pResult)))
{
printf("%s | %s | %s | %s | %s\r\n",Row[0],Row[1],Row[2],Row[3],Row[4]);
}
printf("----------------------------------------");
printf("\r\n\r\n");
mysql_close(pConnection);
}
if(action == 2)
{

printf("\n\nAdding New Homework(CANNOT USE SPACES YET):\n");

printf("Enter Homework Subject:");
scanf("%s",&subject);

printf("Enter homework number:");
cin >> number;

printf("Enter Homework Assignment:");
scanf("%s",&assignment);

//format all the information into a query
mysql_real_connect(pConnection,"localhost","root","*************8","homework",0,NULL,0);
sprintf(Query, "INSERT INTO homework(subject,number,assignment) VALUES('%s','%d','%s')", subject, number, assignment);

printf("Using: %s\n", Query);

if(mysql_query(pConnection,Query) != 0) //execute the query
{
printf("Error Executing the query: %s\n\n",mysql_error(pConnection));
}
else
{
printf("New Homework Assignment Added\n\n");
}
mysql_close(pConnection);
}
if(action == 3)
{
printf("-------------All Homeworks--------------\r\n");
mysql_real_connect(pConnection,"localhost","root","**********","homework",0,NULL,0);
mysql_query(pConnection,"SELECT * FROM homework"); //query the database
pResult = mysql_store_result(pConnection);
printf("ID | Subject | Number | Assignment | Done?\r\n");
while ((Row = mysql_fetch_row(pResult)))
{
printf("%s | %s | %s | %s | %s\r\n",Row[0],Row[1],Row[2],Row[3],Row[4]);
}
printf("----------------------------------------");
printf("\r\n\r\n");
mysql_close(pConnection);

}
//END MYSQL CONNECTION

if(action == 3)
{
printf("-------------All Homeworks--------------\r\n");
mysql_real_connect(pConnection,"localhost","root","************","homework",0,NULL,0);
mysql_query(pConnection,"SELECT * FROM homework"); //query the database
pResult = mysql_store_result(pConnection);
printf("ID | Subject | Number | Assignment | Done?\r\n");
while ((Row = mysql_fetch_row(pResult)))
{
printf("%s | %s | %s | %s | %s\r\n",Row[0],Row[1],Row[2],Row[3],Row[4]);
}
printf("----------------------------------------");
printf("\r\n\r\n");
mysql_close(pConnection);

}
//END MYSQL CONNECTION
if(action == 4)
{

printf("Enter Number to be Marked as Done:");
cin >> numberdone;

//format all the information into a query
mysql_real_connect(pConnection,"localhost","root","*************","homework",0,NULL,0);
sprintf(Query, "UPDATE homework SET status = 'y' WHERE id='%d'",numberdone);
printf("Using: %s\n", Query);

if(mysql_query(pConnection,Query) != 0) //execute the query
{
printf("Error Executing the query: %s\n\n",mysql_error(pConnection));
}
else
{
printf("Marked As Done!\n\n");
}
mysql_close(pConnection);
}
if(action == 3)
{
printf("-------------All Homeworks--------------\r\n");
mysql_real_connect(pConnection,"localhost","root","************","homework",0,NULL,0);
mysql_query(pConnection,"SELECT * FROM homework"); //query the database
pResult = mysql_store_result(pConnection);
printf("ID | Subject | Number | Assignment | Done?\r\n");
while ((Row = mysql_fetch_row(pResult)))
{
printf("%s | %s | %s | %s | %s\r\n",Row[0],Row[1],Row[2],Row[3],Row[4]);
}
printf("----------------------------------------");
printf("\r\n\r\n");
mysql_close(pConnection);
//END 4

}
if(action == 5)
{
printf("-------------Done Homeworks--------------\r\n");
mysql_real_connect(pConnection,"localhost","root","***********","homework",0,NULL,0);
mysql_query(pConnection,"SELECT * FROM homework WHERE status = 'y'"); //query the database
pResult = mysql_store_result(pConnection);
printf("ID | Subject | Number | Assignment | Done?\r\n");
while ((Row = mysql_fetch_row(pResult)))
{
printf("%s | %s | %s | %s | %s\r\n",Row[0],Row[1],Row[2],Row[3],Row[4]);
}
printf("---------------------------------------------");
printf("\r\n\r\n");
mysql_close(pConnection);
}
if(action == 6)
{//if
exit(0);
}//if
}
return 0;
} // end main

Oh, and sorry for not commenting and indenting probably. I never really got the hang of indenting, and commenting is something that doesn't come naturally. Thanks for your help.

___________________

Current Project: Getting a damn Portfolio/Resume Online.

Quick Jump:

Main Navigation


Site & Graphic Design by Aeon Tan
Developed by Jeremie Pelletier & Scott Roach


NeverAPI generated this page in 0.0176 seconds.