Hey. I get bored so muck around with c++ and i have made this:

/* My shop program*/

#include <iostream>
#include <fstream>
#include <conio.h>
#include <time.h>

using namespace std;

int main()
{
    double quantity;
    unsigned int itemNo;
    
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    
    ofstream myfile;
    
    cout << "Please enter the item number : ";
    cin >> itemNo;
    
    switch(itemNo)
    {
                  case '0100':
                       cout << endl << "Quantity? : ";
                       cin >> quantity;
                       cout << 0.5 * quantity;
                       
                       myfile.open ("shop.txt", ios::ate | ios::app);
                       myfile << itemNo << " * " << quantity << asctime (timeinfo);
                       myfile.close();
                       break;
    }
    

    
    getch();
}

It all compiles fine, and everything but i get a [WARNING] multi-character character constant.

What happens is the program executes and displays "Please enter the item number : " so i enter the number and then press enter but then i think getch(); is executed, can someone help me with this little mistake, thanks.

'0100' is not a string. "0100" is.
Only use ' ' for single chars, use "" for strings;

Are you on about my case statement?

You're trying to compare itemNo with a character '0100', don't forget that single quotes mean character and double quotes mean string. But itemNo is an int, so no matter what you type, you're not going to match it. The same goes if you take the single quotes away because 0100 is an octal number that equates to '@' in ASCII. cin will fail if you try to type that.

Edward would use a string instead of an int for the item numbers because then you can have leading 0's:

/* My shop program*/

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <time.h>

using namespace std;

int main()
{
  double quantity;
  string itemNo;

  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

  ofstream myfile;

  cout << "Please enter the item number : ";
  cin >> itemNo;

  if (itemNo == "0100") {
    cout << endl << "Quantity? : ";
    cin >> quantity;
    cout << 0.5 * quantity;

    myfile.open ("shop.txt", ios::ate | ios::app);
    myfile << itemNo << " * " << quantity << asctime (timeinfo);
    myfile.close();
  }

  getch();
}

You can't use strings with a switch statement though.

Thanks, btw on the if statement i tried the "s but got a error, changed to 's and got same error as before

I have got c++ in 21 days though have only read the first 5 days

Quote:

I love this book, its simply fantastic. I have tried another C++ Beginners book and i *thought* c++ was ridiculously hard and i would never acomplish it. ( sams teach yourself c++ in 21 days ) i mean i had read like 2 hundred pages of this book and it was still on the basics - i think i hadn't even reached the cin >> command !! this one however does move at an okay pace. Herb Shild really knows his stuff, sometimes however he explains it a little TOO thoughrouly and i end up having to re-read certain parts of the chapter again. There is a learning cve towards the end of this book though. it suddenly becomes a lot more comlicated once you hit the pointers, although im pretty sure this is because i had a little experience in the BASIC language, and nothing at all like pointers ever came up. I attempt to read 1 chapter in a go, although after i feel my brain has been fried in the latter parts of the book.

EDIT: Thanks for posting link to book, asked parents and might be allowed book soon, btw is it a book or e-book?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.