New to C++ Help Change from Purchase/Date Class

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 1295 Views - Last Post: 03 September 2013 - 09:05 PM Rate Topic: -----

#1 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 02:46 PM

Hello everyone :D/>/>

So I am working on a problem that is defined by a couple of things I will get to in a moment. First I am having issues with the language of what I am doing when asked to do something such as: "Write specifications for a function that computes the change (you'll understand the change reference in a moment) that you should receive or shouldn't receive. Include a statement of purpose, the preconditions and postconditions and a description of the arguments" I thought at first my professor was asking me to write something out on paper and later realized I have a huge lack of knowledge in the general terminology used in the language and what he seems to be asking is for pieces of a program hah, can someone confirm this? "The price of an item you want to buy is given in (d) dollars and © cents." This is all of the information provided.

Note: I am using Dev-C++ for my compiler.

#include <iostream>

using namespace std;

int main()
{
    
int c;
int d;
int Costd(10);
int Costc(20);

cout << 'The price of this item you would like to buy is $10.20' << endl;
cin >> 'With cash how much would you like to pay and I will get your change, if any? Dollars ' >> int d >> endl;
cin >> 'Any cents? ' >> int c >> endl;

// Heres where I am stuck kind of. I don't understand why asking us to break the the dollars and cents down into Integers when my logic tells me a simple float would be a much better idea? At this point I have the amounts of dollars and cents stored in the int c and d correct? So now to compare them to int Costc and Costd right? And then return a change amount or an else statement that would return you have not paid for enough or does it seem like I'm way off base and I should be taking in a user input amount of what the item should cost and the money they have and then compare against that? 

do
{
cout << '\n' <<'Press the Enter key to continue.'
} 
while (cin.get() != '\n')
return 0;
}



I am receiving errors on lines 13 and 14, which are my cin commands "character constant to long for its type."

Like I said heavy beginner here. I am also having a problem understanding what is defined in a .h file when making say a class Date to take in a user input date and then return it one day later. I'm on overload... NOTHING like writing html, xhtml ://>

Is This A Good Question/Topic? 0
  • +

Replies To: New to C++ Help Change from Purchase/Date Class

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 02:59 PM

Single quotes are used for single characters. Double quotes are used for strings of characters.

.h files should contain the class definition, .cpp files the implementation.

So something like

my_date.h = date class definition
my_date.cpp = implementation of functions defined in my_date.h
main.cpp = file which includes the .h, creates an instance of the my_date object and does whatever you want to with that object.

Compile main.cpp and my_date.cpp together (how you do this depends on your environment) to create an executable file.

If you look in the tutorials, I'm pretty sure there's one on this concept.

EDIT: Oh, I noticed you're using Dev-C++. DON'T. It's old, buggy, and unmaintained. If you MUST use Dev-C++, at least use the updated, maintained version Orwell Dev-C++.

This post has been edited by JackOfAllTrades: 01 September 2013 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 03:58 PM

Ah! So the current ' in the cin and cout should be changed to " because I am writing "Blah Blah Blah Etc Etc Etc" rather than just writing 'A'?

Also then my understanding of what you said about .cpp files and .h files is that the compiler will handle the main and the resource files when compiling the "project" and that there is no code needed to "link" the .h file to the .cpp file?

ALSO I am not bound to using Dev as my compiler it was only provided for free through the course I am taking. Is Orwell the best way for me to go or is there a better FREE compiler available that I should be working with? Although I hate it, or atleast what I've seen of it so far, I do have Visual Express Desktop 2012 is that the best way to go?

I will jimmy through my coding for now comeback if I have any other problems. And just to be clear I have called the c and d int from my cin coding correctly?
Was This Post Helpful? 0
  • +
  • -

#4 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 04:24 PM

Ok so I have changed the ' to " in my cin and cout commands. And fixed that error but now I am receiving these errors:

C:\Dev-Cpp\maintest.cpp In function `int main()':

15 C:\Dev-Cpp\maintest.cpp ambiguous overload for 'operator>>' in 'std::cin >> "Dollars:"'

note C:\Dev-Cpp\include\c++\3.4.2\istream:687 candidates are: std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*) [with _Traits = std::char_traits<char>] <near match>

note C:\Dev-Cpp\include\c++\3.4.2\istream:687 std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*) [with _Traits = std::char_traits<char>] <near match>

15 C:\Dev-Cpp\maintest.cpp expected primary-expression before "int"
15 C:\Dev-Cpp\maintest.cpp expected `;' before "int"
17 C:\Dev-Cpp\maintest.cpp expected primary-expression before "int"
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 04:31 PM

Please post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 04:31 PM

Post #4 Continued

17 C:\Dev-Cpp\maintest.cpp expected `;' before "int"
C:\Dev-Cpp\Makefile.win [Build Error] [maintest.o] Error 1

after changing my coding to as follows:


#include <iostream>  

using namespace std;  

int main()  
{   

int c;  
int d;  
int Costd(10);  
int Costc(20);      

cout << "The price of this item you would like to buy is $10.20" << endl;  
cin >> "Dollars:" >> int d >> endl; 
cout << "Any cents?" << endl;  
cin >> int c >> endl;

system("PAUSE");
return EXIT_SUCCESS;
}



Are my cin and out statements of coding just incorrect altogether? I thought that using >> Int d was an acceptable way to assign a userinput number to my int c or int d? Am I wrong altogether?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 04:41 PM

Quote

Are my cin and out statements of coding just incorrect altogether?

Basically yes. The cin stream is input only, you can't use it to output text. Next you can't define a variable in the cin stream. Lastly there is no endl defined for input streams, this is an output only method.
//cin >> "Dollars:" >> int d >> endl; // Won't compile. Try: 
int d;
cout << "Dollars:";
cin >> d;




Jim
Was This Post Helpful? 0
  • +
  • -

#8 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 04:49 PM

Ok so endl; only with cout and cin only defines a string? in otherwords the console can print both the cin and cout in the same "line"?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 05:10 PM

No cin is for input, you supply the value. You can use cin for any type that can have input operations. For example std::string, int, double, etc.

Jim

This post has been edited by jimblumberg: 01 September 2013 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 08:10 PM

Ok this is my most up to date code. The program compiles and seems to have all the information it needs to run the algorithms I just seem to either have my loops completely messed up or? At this point I'm completely lost. It is returning no error codes. Can anyone see what I've done horribly wrong here?


#include <iostream>  

using namespace std;  

int main()  
{   

int costc;
int costc2;  
int costd; 
int costd2;      

cout << "The price of this item you would like to buy is $10.20" << endl;
cout << "How much money do you have in dollars and cents?" << endl;
cout << "Dollars: $";
cin >> costd;

if (costd < 10)
{
   cout << "I'm sorry this is not enough money for this item." << endl;
   cout << "A different amount of dollars perhaps?";
   cin >> costd;
}

else if (costd > 10)
{
   costd2 = costd - 10;
   costc2 = 100 - 20;
   cout << "Your change is "<< costd2 << " dollars and "<< costc2 << " cents." << endl;
   cout << "Thank you for your purchase." << endl;
}

else if (costd == 10)
{
   cout << "Any cents?: ";
   cin >> costc;
   
   if (costc > 20)
   {
      costc2 = costc - 20;
      costd2 = 0;
      cout << "Your change is " << costd2 << " dollars and " << costc2 << " cents." << endl;
      cout << "Thank you for your purchase today." << endl;
   }
   
   else if (costc < 20)
   {
      cout << "I'm sorry this is not enough money for this item." << endl;
      cout << "A different amount of cents perhaps?" << endl;
      cin >> costc;
   }
   
   else (costc == 20);
   {
      cout << "Thank you for your purchase. You have no change today." << endl;
   }
}

system("PAUSE");
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 10:21 PM

What are you inputting into your program?

Jim
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,748
  • Joined: 05-May 12

Re: New to C++ Help Change from Purchase/Date Class

Posted 01 September 2013 - 10:33 PM

And to answer your questions here:

Quote

Heres where I am stuck kind of. I don't understand why asking us to break the the dollars and cents down into Integers when my logic tells me a simple float would be a much better idea? At this point I have the amounts of dollars and cents stored in the int c and d correct? So now to compare them to int Costc and Costd right? And then return a change amount or an else statement that would return you have not paid for enough or does it seem like I'm way off base and I should be taking in a user input amount of what the item should cost and the money they have and then compare against that?


One of the things you should learn is that money should not be stored in floating point variables. The primary reason is that way floating point numbers are implemented in computers is just an approximation and errors creep in as you perform computations on them. The second reason is that you will learn that comparing two floating point numbers is not precise. To do comparisons correctly, you'll need to compare two floating point numbers with plus or minus some allowable error. (Much of what is in "What Every Computer Scientist Should Know About Floating Point Arithmetic" may go over your head now, but scan through it now, and bookmark it for reading later as you gain more experience.)

The other major reason, though, is that in a later assignment, you'll be faced with figuring out how to return the change in various denominations. So if you had denominations of 100, 50, 20, 10, 5, 1 dollar bills, and 50, 25, 10, 5, and 1 cent coins, you'll need to figure how many of each bill and each coin should be given out. Computing that is much easier when using integers rather than floating point.
Was This Post Helpful? 0
  • +
  • -

#13 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 09:59 AM

View PostSkydiver, on 01 September 2013 - 10:33 PM, said:

And to answer your questions here:

Quote

Heres where I am stuck kind of. I don't understand why asking us to break the the dollars and cents down into Integers when my logic tells me a simple float would be a much better idea? At this point I have the amounts of dollars and cents stored in the int c and d correct? So now to compare them to int Costc and Costd right? And then return a change amount or an else statement that would return you have not paid for enough or does it seem like I'm way off base and I should be taking in a user input amount of what the item should cost and the money they have and then compare against that?


One of the things you should learn is that money should not be stored in floating point variables. The primary reason is that way floating point numbers are implemented in computers is just an approximation and errors creep in as you perform computations on them. The second reason is that you will learn that comparing two floating point numbers is not precise. To do comparisons correctly, you'll need to compare two floating point numbers with plus or minus some allowable error. (Much of what is in "What Every Computer Scientist Should Know About Floating Point Arithmetic" may go over your head now, but scan through it now, and bookmark it for reading later as you gain more experience.)

The other major reason, though, is that in a later assignment, you'll be faced with figuring out how to return the change in various denominations. So if you had denominations of 100, 50, 20, 10, 5, 1 dollar bills, and 50, 25, 10, 5, and 1 cent coins, you'll need to figure how many of each bill and each coin should be given out. Computing that is much easier when using integers rather than floating point.


Read over it a little bit and thank you for the resource, I'm sure I am going to need it, but just your explanation of the denominations used in money is enough for me to understand why money should not be stored in a float. The professor had already talked about floats and such having issues with being completely correct when compared against each other I just had thought a simple xx.xx amount should be small enough to do under a float. Has anyone found the issue with my loop or have I missed something about returning my loop to the top after going through or should I be using a for loop?
Was This Post Helpful? 0
  • +
  • -

#14 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 651
  • View blog
  • Posts: 2,234
  • Joined: 31-December 10

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 11:56 AM

Lines 13-57 should be wrapped in a while loop.
Was This Post Helpful? 0
  • +
  • -

#15 xcrbprame87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-September 13

Re: New to C++ Help Change from Purchase/Date Class

Posted 02 September 2013 - 04:15 PM

View Postvividexstance, on 02 September 2013 - 11:56 AM, said:

Lines 13-57 should be wrapped in a while loop.


Awesome! Thank you I knew it was something to do with my loops causing it to not work correctly.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3