GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2009, 10:03
tytanic11 tytanic11 is offline
New Member
 
Join Date: Nov 2009
Posts: 4
tytanic11 is on a distinguished road

Just starting C++


I'm taking an introductory c++ course at my university; our project this week is a program that calculates a shipping/merchandise cost for sales. I'm not a computer science major or anything, so I have very little programming background. I've included comments that hopefully explain what each function is supposed to do. I've also included the compile errors below. I haven't written all of the functions, because I'm stuck on the compile errors.

CPP / C++ / C Code:
{


#include <iostream>
using namespace std;

  /* Setting up the function prototypes */
  void instructions ();
  int spoolsordered ();
  int instock ();
  int spoolssent (int instock, int ordered);
int backorder (int ordered, int sent);
  int subtotal ();
  int shippingcost ();
  int totalcost ();
  int results ();

/* Main function */

int main ()
{


 instructions ();
 spoolsordered ();
 instock ();
 spoolssent(instock, ordered);
 backorder (spoolsordered,spoolssent);
  return 0;
}

/* Set up the function definitions */
/* Print the instructions for the user */
void instructions ()
{ cout << " Welcome to WholeSale order calculator v1.0 \n This program will calculate the total cost of an order based on your input. \n Please respond to t\
he prompts below:\n ";
}
/*Ask how many spools were ordered, don't accept less than 1 */
int spoolsordered ()
{ int ordered;
cout << "How many spools were ordered? ";
  cin >> ordered;
  if (ordered < 1)

   {cout << "Please enter a value greater than one.\n";
   cout << "How many spools were ordered? ";
   cin >> ordered; }
else
 return ordered;
}
/*Ask the user how many spools are in stock, don't accept less than zero */
int instock ()
{ int instock;
cout << "How many spools are currently in stock? ";
 if (instock < 0)
   {cout << "Please enter a value greater than 0.\n";
    cout << "How many spools are currently in stock?";
    cin >> instock;
   }
 cin >> instock;
    return instock;
}
/*Determine how many spools will be shipped now, by comparing the number ordered to the number in stock */
int spoolssent (int instock, int ordered)
{int sent;
  if (instock>ordered)

     ordered=sent;
  else if (ordered > instock)
    instock=sent;
  else
    cout << "Something is wrong!\n";
  return sent;
}
/* Determine and pass the number of backordered units */
int backorder (int ordered, int sent)
{ int backordered = ordered-sent;
  return backordered;
}
/*Multiply the number of units shipped by spoolssent() by the per unit cost */
int subtotal ()
{}
/*Multiply the number of units being shipped by the shipping cost*/
int shippingcost ()
{}
/*Add subtotal and shipping cost together, to get the total cost to the buyer */
int totalcost ()
{}
/*Print the results in an organized fashion for the user */
int results ()
{}

> g++ wholeSale_.cpp
wholeSale_.cpp: In function `int main()':
wholeSale_.cpp:28: error: `ordered' was not declared in this scope
wholeSale_.cpp:29: error: invalid conversion from `int (*)()' to `int'
wholeSale_.cpp:29: error: initializing argument 1 of `int backorder(int, int)'
wholeSale_.cpp:29: error: invalid conversion from `int (*)(int, int)' to `int'
wholeSale_.cpp:29: error: initializing argument 2 of `int backorder(int, int)'
  #2  
Old 16-Nov-2009, 13:36
TanLorik TanLorik is offline
Junior Member
 
Join Date: Nov 2009
Posts: 38
TanLorik has a spectacular aura aboutTanLorik has a spectacular aura about

Re: Just starting C++


lol, soo many functions...
I'll take it bit by bit

CPP / C++ / C Code:
{


#include <iostream>

there shouldn't be anything before #include (not at this level at least), make sure to delete everything before #include.

next bit is hard to explain on your code so I'll make a new example
CPP / C++ / C Code:
int function()
{int variable = 10;       //variable is available from here
return variable;
}                             // to here where the function ends
int function2(int variable){ bla bla...}

int main()
{function();
function2(variable);} 


when I declared variable, it's availability domain is just function, and outside fuction, it doesn't exist. In your program since you keep using the same variables, I suggest
1. using global variables (declare them outside of functions, and they will be available everywhere)
2. since all your code is liniar, you can just write it in main, without the use of so many functions
3.keep some exchange variables in main, like

CPP / C++ / C Code:
int main ()
{
int a,b,c; // used a,b,c to differantiate from functions.

 instructions ();
a= spoolsordered ();
b= instock ();
c= spoolssent(a , b);
 backorder (a , c);
  return 0;
}

I think the other errors are caused by the inavailability of the variables, try using one of my suggestions and post again.

Hope this helps
  #3  
Old 18-Nov-2009, 03:45
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Regular Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 342
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Just starting C++


Quote:
Originally Posted by tytanic11
I'm taking an introductory c++ course at my university; our project this week is a program that calculates a shipping/merchandise cost for sales. I'm not a computer science major or anything, so I have very little programming background. I've included comments that hopefully explain what each function is supposed to do. I've also included the compile errors below. I haven't written all of the functions, because I'm stuck on the compile errors.

CPP / C++ / C Code:
{


#include <iostream>
using namespace std;

  /* Setting up the function prototypes */
  void instructions ();
  int spoolsordered ();
  int instock ();
  int spoolssent (int instock, int ordered);
int backorder (int ordered, int sent);
  int subtotal ();
  int shippingcost ();
  int totalcost ();
  int results ();

/* Main function */

int main ()
{


 instructions ();
 spoolsordered ();
 instock ();
 spoolssent(instock, ordered);
 backorder (spoolsordered,spoolssent);
  return 0;
}

/* Set up the function definitions */
/* Print the instructions for the user */
void instructions ()
{ cout << " Welcome to WholeSale order calculator v1.0 \n This program will calculate the total cost of an order based on your input. \n Please respond to t\
he prompts below:\n ";
}
/*Ask how many spools were ordered, don't accept less than 1 */
int spoolsordered ()
{ int ordered;
cout << "How many spools were ordered? ";
  cin >> ordered;
  if (ordered < 1)

   {cout << "Please enter a value greater than one.\n";
   cout << "How many spools were ordered? ";
   cin >> ordered; }
else
 return ordered;
}
/*Ask the user how many spools are in stock, don't accept less than zero */
int instock ()
{ int instock;
cout << "How many spools are currently in stock? ";
 if (instock < 0)
   {cout << "Please enter a value greater than 0.\n";
    cout << "How many spools are currently in stock?";
    cin >> instock;
   }
 cin >> instock;
    return instock;
}
/*Determine how many spools will be shipped now, by comparing the number ordered to the number in stock */
int spoolssent (int instock, int ordered)
{int sent;
  if (instock>ordered)

     ordered=sent;
  else if (ordered > instock)
    instock=sent;
  else
    cout << "Something is wrong!\n";
  return sent;
}
/* Determine and pass the number of backordered units */
int backorder (int ordered, int sent)
{ int backordered = ordered-sent;
  return backordered;
}
/*Multiply the number of units shipped by spoolssent() by the per unit cost */
int subtotal ()
{}
/*Multiply the number of units being shipped by the shipping cost*/
int shippingcost ()
{}
/*Add subtotal and shipping cost together, to get the total cost to the buyer */
int totalcost ()
{}
/*Print the results in an organized fashion for the user */
int results ()
{}

> g++ wholeSale_.cpp
wholeSale_.cpp: In function `int main()':
wholeSale_.cpp:28: error: `ordered' was not declared in this scope
wholeSale_.cpp:29: error: invalid conversion from `int (*)()' to `int'
wholeSale_.cpp:29: error: initializing argument 1 of `int backorder(int, int)'
wholeSale_.cpp:29: error: invalid conversion from `int (*)(int, int)' to `int'
wholeSale_.cpp:29: error: initializing argument 2 of `int backorder(int, int)'


Compare your code to the following:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

/* Setting up the function prototypes */
void instructions ();
int spoolsordered ();
int instock ();
int spoolssent (int instock, int ordered);
int backorder (int ordered, int sent);
int subtotal ();
int shippingcost ();
int totalcost ();
int results ();

/* Main function */

int main ()
{
    instructions ();
    int ordered = spoolsordered();
    int stock = instock();

    int sent = spoolssent(stock, ordered);
    if(sent < ordered)
    {
        backorder (ordered, sent);
    }
    return 0;
}

/* Set up the function definitions */
/* Print the instructions for the user */
void instructions ()
{
    cout << " Welcome to WholeSale order calculator v1.0 \n This program will calculate the total cost of an order based on your input. \n Please respond to t\
he prompts below:\n ";
}

/*Ask how many spools were ordered, don't accept less than 1 */
int spoolsordered ()
{
    int ordered;
    cout << "How many spools were ordered? ";
    cin >> ordered;
    if(ordered < 1)
    {
        cout << "Please enter a value greater than one.\n";
        cout << "How many spools were ordered? ";
        cin >> ordered;
    }
    return ordered;
}

/*Ask the user how many spools are in stock, don't accept less than zero */
int instock ()
{
    int instock;
    cout << "How many spools are currently in stock? ";
    if(instock < 0)
    {
        cout << "Please enter a value greater than 0.\n";
        cout << "How many spools are currently in stock?";
        cin >> instock;
    }
    cin >> instock;
    return instock;
}
/*Determine how many spools will be shipped now, by comparing the number ordered to the number in stock */
int spoolssent (int instock, int ordered)
{
    int sent;
    if(instock > ordered)
        ordered=sent;
    else if(ordered > instock)
        instock=sent;
    else
        cout << "Something is wrong!\n";
    return sent;
}
/* Determine and pass the number of backordered units */
int backorder (int ordered, int sent)
{
    int backordered = ordered-sent;
    return backordered;
}
/*Multiply the number of units shipped by spoolssent() by the per unit cost */
int subtotal ()
{
    return 0;
}

/*Multiply the number of units being shipped by the shipping cost*/
int shippingcost ()
{
    return 0;
}
/*Add subtotal and shipping cost together, to get the total cost to the buyer */
int totalcost ()
{
    return 0;
}

/*Print the results in an organized fashion for the user */
int results ()
{
    return 0;
}


...while this compiles "cleanly," it doesn't perform as you probably intend.

Part of your problem started with the very first line where you have an opening brace. Then you tried to pass a function address (function pointer) as an integer value to a couple of functions, which is obviously not what you intended.

My changes include adding a couple of variables (not globals!) to hold the results of your program flow that fetches information from the user.

Also, you do not want to specify a function that returns a value without actually returning a value.

Increase your warning level using the following:

g++ -g -Wall -W -pedantic -o wholeSale_ wholeSale_.cpp

The -g switch enables debugging symbols. You will certainly want to use a debugger in your development process.

A better idea is to write a main that does nothing but return a zero. Compile it and test it. Add a very small bit of functionality, compile, execute/test, debug. Keep going by adding a very small bit of new functionality each time.

By adding a whole bunch of "stuff at one time" you risk not ever knowing what part is causing issues. Better is to build incrementally on small successes, particularly until you get the hang of things.

So, in your code, you would have found that the first call in main to "spoolssent" would have blown up...because that would have been the "next" thing that you added as the others would have succeeded (assuming you removed the brace on the first line). Then you would have known to investigate that one line and resolve it before continuing.

As pointed out by a previous poster, variable names of "a, b, c" are rarely a very good choice. If you're writing a program that works with trigonometry, they're probably useful, meaningful names. Just about everywhere else, they're going to be much less indicative of the thinking (or lack thereof) that went into their declarations.

While commonplace to newcomers, coding style issues tend to be plentiful and lack good consistency. Here are a few style choices that you may want to consider employing very early on. I find that they help reduce bugs by making the code more consistent and easier to develop and maintain over time, which can be even a few minutes or much longer.

1: Always use "multi-line" style conditional statements (this means to add braces after every if..else..while, etc.), even if the next line is the only line, which would be a "single-line" conditional statement.

2: Find a reliable, common variable naming convention. Remember that your function names are "variables" in a sense, too. They are "variable" in the sense that they need to be descriptive and they need to be easy to read and that you're the one telling us about them for the first time, so come up with a naming convention that works. Plentiful examples abound.

3: Always use spaces between operators. That means, don't do a+b=c; Use a + b = c; instead.

4: Prefer to use more variables than fewer. If you are going to call a function that returns a value, declare a variable of the return type and store the result of the function call in that variable.

5: Step through your code with a debugger. While some prefer to use "cout" or "printf" almost exclusively, the benefit of using a debugger will greatly enable you should you ever do anything more than the most trivial coding. At the bare minimum, if you do #4 above, it is much easier to cout and printf your "status" as you execute code. Just remember, while cout/printf may be your "friends," they make code fatter, harder to maintain and dramatically increase required system resources and code execution time. On some platforms, this may be unimportant. On others, it is impossible.

Develop a coding style (prefer a common style over your own, homegrown style choices) and stick with it...at least for now. Depart when you're ready and have some meaningful reason to change styles. At some future point, you may want to get used to coding using any of perhaps 5 to 7 of the most common coding styles. Being style agnostic is a good thing, as long as the style is consistently applied and doesn't "freak out" other programmers.


MxB
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Starting own Web Design Small Business. harypo Web Design Forum 3 12-Nov-2008 07:58
cPanel Hosting starting at ONLY $3.95/mo - FREE SETUP - RVSkin & FANTASTICO WireNine Web Hosting Advertisements & Offers 0 20-Feb-2007 09:59
cPanel Hosting starting at ONLY $3.95/mo - FREE SETUP - RVSkin & FANTASTICO WireNine Web Hosting Advertisements & Offers 0 13-Feb-2007 12:50
Problem starting up with my Vaio Vaio007 Computer Hardware Forum 1 06-Sep-2005 12:09
Starting Mysql server problem pjacks MySQL / PHP Forum 23 08-Sep-2004 17:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:16.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.