Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-05-2007, 02:23 PM   #1 (permalink)
tiggerzone
Recruit
 
Join Date: Feb 2007
Posts: 18
tiggerzone is on a distinguished road
NEw to coding in C++ and need help

I was given a few exercises from the text book and I understand how to write the IPO charts but when it comes to coding still having problems understanding it can anyone help me I'm taking a introducton course to programing with C++ so if anyone can help I would appreciate it.


Exercise-
Builders Inc. needs a program that allows the company's salesclerks to enter both the diameter of a circle ( in feet) and the price of the railing material per foot. The diameter and price may contain decimal places. The program should display both the circumference of the circle and the total price of railing material. Use 3.141593 as the value of pi.


can anyone help me find this code or help assist me in getting it completed.
Like I said I'm new to this and really could use the help.
tiggerzone is offline   Reply With Quote
Old 02-05-2007, 02:55 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
It's pretty simple, if you take a look at the answer I provided last, use the same form to begin the program ie:
Code:
#include <iostream>

int main()
{
  const float pi = 3.141593;
Since we need to use some arrithmetic which involves dicimal numbers, we need to declare them as floats.

The math in this, is a well known formular, which we all probably learned in 4.th grade or there about.. Where you can calculate the circumference if you know the diameter, and you have PI handy.

Since we need to make a nice display of the result, it would be wise to use som I/O manipulation provided from iomaip.h like forinstance precision().

Combining this we end up with something like this:
Code:
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  const float pi = 3.141593;
  float diameter=-1, price=0;
  while(diameter < 0){
    cout << "Please enter the diameter of the circle: ";
    cout.flush();
    cin >> diameter;
    if(diameter < 0)
      cout << "Sorry it's impossible to have a negative diameter" << std::endl;
  }
  cout << "Please enter the price of the railing material: ";
  cout.flush();
  cin >> price;
  cout << fixed << showpoint << setprecision(2);
  cout << "The circumference is: " << /* here you continue... */
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-05-2007, 03:04 PM   #3 (permalink)
tiggerzone
Recruit
 
Join Date: Feb 2007
Posts: 18
tiggerzone is on a distinguished road
hey there

your coding is right but is too advanced from what we are learning right now is there a easier way to code it.
tiggerzone is offline   Reply With Quote
Old 02-05-2007, 03:17 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
Quote:
your coding is right but is too advanced from what we are learning right now
Then give some advice to what level you're at right now..
At the moment I assume you know of the cout/cin streams, and how you can use them..

The iomanip part, is just to make sure the result will be nicely displayed with two decimals showing even if the result should be 34221.89237 it will only show 34221.89 Which would make it more appropriate to say, "the price is $34.80" instead of something like "the price is $34.804221"

The while() loop which is surrounding the initial diameter reading, is a way to make sure the user is forced to input a positive value, since you can't have a negative diameter of any circle no matter how hard you try (4.th dimensions not enclosed)

The use of the word const is to make sure the pi value can never be changed..

Oh and by the way, the formular I'm refering to is the old Pi*diameter like any school kid should know by 4'th or 5'th grade if I remember correct..

If you want me to explain the code line by line, then just say so, and I'll give it my best.

If it's the cout.flush() that is throwing you off, then they are used to flush teh output stream, in order to make sure any charactor is displayed on teh screen befor accepting any input, since in teh previus lines I'm not using std::endl to flsuh teh stream.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-05-2007, 03:34 PM   #5 (permalink)
tiggerzone
Recruit
 
Join Date: Feb 2007
Posts: 18
tiggerzone is on a distinguished road
what I learned so far

float
constant
assignment operator
getline () funtion
arguments
ignore ()
newline character


some thing like this :
example from text book


# include <iostream>
# include <string>


using std: : cout ;
using std: : cin ;
using std: : end l;
using std: : string ;


int main ()
{

//declare variables
double sales = 0.0 ;
string stateName = " " ;


// enter input items
cout << "Enter the sales amount : " ;
cin >> sales ;
cin.ignore (100, '\n') ;
cout << "Enter the state name : " ;
getline ( cin, stateName) ;


//display output items
cout << " You entered sales of " << sales ;
cout << " for " << stateName << end ;


return 0 ;
} // end of main function




(this was an example in the text book they gave us )
tiggerzone is offline   Reply With Quote
Old 02-05-2007, 03:45 PM   #6 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
If you compare that example to what I'm showing you, you almost have the same level..
They use using std::cout etc. to tell when ever you use cout it's the one from the std::i/o class.. I use using namespace std to tell it, when ever something like cout/cin/fixed/showpoint dosn't fit anything it is assumed to come from the std::i/o class.

When they declare variables, they assign a value to them, like I do to mine..
They ask the user a few questions, and reads the response like I do.. They use the ignore() function, to make sure teh user dosn't type in a lot of garbage to find a loop hole in the code, I'm pretty ignorant to that, since this is a beginners class..

They display the results to the user, this part I'm hoping you are capable of compleeting yourself..

All in all, what I can see as a difference, is that you havn't learned loops yet, and I'm usign the while() loop to detect any unwanted input, but apart from that, the level seems about right.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-05-2007, 11:33 PM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
And since I didn't understand your request in the latest thread I'll provide hte missing part here aswell..
Code:
                                      pi*diameter << endl;
  cout << "The cost for the railing will be: $" << pi*diameter*price << endl;
  return 0;
}
As you can see, theres not that much into it..

Next assignment I wont be that helpful, and I'll much rather try to talk you through it.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 02-06-2007, 11:04 AM   #8 (permalink)
tiggerzone
Recruit
 
Join Date: Feb 2007
Posts: 18
tiggerzone is on a distinguished road
I was able to complete that question and ran it and it had no bugs so thanks. Are you good with flow charts?
tiggerzone is offline   Reply With Quote
Old 02-07-2007, 09:35 AM   #9 (permalink)
tiggerzone
Recruit
 
Join Date: Feb 2007
Posts: 18
tiggerzone is on a distinguished road
I have completed this exercise.
tiggerzone is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:17 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting