Hey guys, I have an assignment that I have been working on for hours and I've done a lot of testing and I finally got it working, but when I try to change it to make sure to prompt the user for an input I can't get it to work correctly.
Assignment is Below:
For this assignment, you will write a C/C++ program to print out information from the Premiere database.
You are to report on the most recent order for a particular salesrep. Prompt the user to enter a salesrep number. For that salesrep’s latest order, print the order number, date, customer number, name and address, and a list of items ordered. For each item, list the item number, description, number ordered, unit price and total amount for the item (number ordered*unit price). After all of the items are listed, print out the total amount owed for the order (add up all of the total amounts for the order).
I'm just so fustrated I can get it to work correctly.
#include <iostream>
#include <mysql.h>
using namespace std;
//To create connection and store query results
MYSQL * connection;
MYSQL_RES * result;
MYSQL_ROW row;
int query_state;
int main()
{
//Variables to connect to MYSQL
char * server = "s*****";
char * user = "******";
char * password = "*****";
char * database = "*********";
int i = 1;
connection = mysql_init(NULL);
//Connecting to MYSQL
if (!mysql_real_connect(connection,server,user,password,database,0,NULL,0))
{
cout << mysql_error(connection) << endl;
return 1;
}
//First query
if (mysql_query(connection, "select ORDERS.ORDER_NUM,ORDERS.ORDER_DATE,CUSTOMER.CUSTOMER_NUM,CUSTOMER.CUSTOMER_NAME,CUSTOMER.STREET,CUSTOMER.CITY,CUSTOMER.STATE,REP.LAST_NAME,REP.FIRST_NAME,PART.PART_NUM,PART.DESCRIPTION,ORDER_LINE.NUM_ORDERED,PART.PRICE,ORDER_LINE.NUM_ORDERED*PART.PRICE as TOTAL_AMOUNT from CUSTOMER,ORDERS,ORDER_LINE,PART,REP where CUSTOMER.CUSTOMER_NUM=ORDERS.CUSTOMER_NUM and CUSTOMER.REP_NUM=REP.REP_NUM and ORDERS.ORDER_NUM=ORDER_LINE.ORDER_NUM and ORDER_LINE.PART_NUM=PART.PART_NUM order by ORDERS.ORDER_NUM,ORDERS.ORDER_DATE"))
{
cout << mysql_error(connection) << endl;
return 1;
}
//Store result
result = mysql_use_result(connection);
cout << "All Information from Orders Table" << endl << endl;
//Print result
while ((row = mysql_fetch_row(result)) != NULL)
{
cout << "Order " << i++ << endl;
cout << "Order Num: " << row[0] << "\tOrder Date: " << row [1]
<< "\tCust. Num: " << row[2] << endl;
cout << "Customer Name: " << row[3] << endl;
cout << "Street: " << row[4] << endl;
cout << "City: " << row[5] << "\tState: " << row[6] << endl;
cout << "Rep. Last Name: " << row[7]
<< "\tRep. First Name: " << row[8] << endl;
cout << "Part Num: " << row[9] << "\tDescprition: " << row[10]
<< "\tNumOrdered: " << row[11] << endl;
cout << "Price: " << row[12] << endl;
cout << "Total Amount: " << row[13] << endl << endl;
}
//Second query
if (mysql_query(connection, "select sum(ORDER_LINE.NUM_ORDERED*PART.PRICE)TOTAL_AMOUNT from ORDERS,ORDER_LINE,PART where ORDERS.ORDER_NUM=ORDER_LINE.ORDER_NUM and ORDER_LINE.PART_NUM=PART.PART_NUM"))
{
cout << mysql_error(connection) << endl;
return 1;
}
//Store and print second result
result = mysql_use_result(connection);
while ((row = mysql_fetch_row(result)) != NULL)
{
cout << "The Total sum of the orders is: " << row[0] << endl;
}
//Free memory used to store result and close connection to MYSQL
mysql_free_result(result);
mysql_close(connection);
return 0;
}