Thread: Question involving using math operations...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    15

    Question involving using math operations...

    Alright, I had posted a while ago about a project asigned to us by a teacher involving payrolls and everything. Well, I ditched the whole way we were doing it and started over with something that I hope will work better and it won't open all the files at once. Well, here's my question, I would like to multiply two values in 2 seperate files and have it displayed in another statement. I tried something simple:

    Code:
    ammount=buffer*5+buffer2;
    printf("The total ammount of money owed to Matt is %i.\n",ammount);
    buffer and buffer2 are the variables where the numbers in the files are stored. I was wondering why this won't work and if there might be a better way to do it (personally, I don't like to use the printf statement, as it looks sloppy to me). I get an error that says:
    error C2296: '*' : bad left operand
    I'm just wondering, thanx...

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Code:
    amount = buffer * 5 + buffer2;
    works for me.
    How have you declared amount and buffer and buffer2.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    15
    yup.. I have them defined as an int and charters. uuuummm... I'll post the code I have so far for that section. Maybe it's something in the code that's doing it...

    Code:
    int mattbview()
    {
      char buffer[256];
      char buffer2[256];
      int ammount;
      ifstream mattb ("files/mattb.txt");
      ifstream mattb2 ("files/mattb2.txt");
      if (! mattb.is_open() && mattb2.is_open())
      { 
    	  cout << "Error opening file"; 
    	  EXIT 
      }
    
      while (! mattb.eof() )
      {
        mattb.getline (buffer,100);
    	mattb2.getline (buffer2,100);
    	cout << endl << "Name:  Matt Bilcliff";
    	cout << endl << "Pay $5/hour";
    	cout << endl << "Matt has worked "; 
        cout << buffer;
    	cout << " hours.";
    	cout << endl << "The boss has given a bonus of ";
    	cout << buffer2;
    	cout << " dollars";
    	ammount = buffer * 5 + buffer2;
    	printf("The total ammount of money owed to Matt is %i.\n",ammount);
    	mattb.close();
    	mattb2.close();
      }
      return 0;
    }
    Yes, it's not the cleanest mess of anything, but it's a start for now. I'll slim it down and try different things out later. lol.. Noticed that I like the cout to display the text?

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    buffer and buffer2 should either be integers or floating point numbers as you can't multiply character arrays.
    zen

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    15
    alright. Thanx for telling me. Maybe I can get it working now.


    [edit]
    When I change char to int and have int buffer; instead of char buffer[256];, I get an error that says:
    error C2664: 'class istream &istream::getline(char *,int,char)' : cannot convert parameter 1 from 'int' to 'char *'
    Any ideas? I believe that it has something to do with this part:

    Code:
    mattb.getline (buffer,100);
    mattb2.getline (buffer2,100);
    But I'm not positive
    Last edited by Screwz Luse; 12-04-2001 at 04:09 PM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Leave buffer and buffer2 as char arrays, and change this:
    ammount = buffer * 5 + buffer2;

    to this:
    ammount = atoi(buffer) * 5 + atoi(buffer2);

    Function atoi() is in <stdlib.h>.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    15
    Originally posted by swoopy
    Try this:
    Leave buffer and buffer2 as char arrays, and change this:
    ammount = buffer * 5 + buffer2;

    to this:
    ammount = atoi(buffer) * 5 + atoi(buffer2);

    Function atoi() is in <stdlib.h>.
    Thanks a lot. It worked perfectly. Now I gotta figure out how to stop the text from repeating. I recieve the following:

    Name: Mat Bilcliff
    Rate: $5/hour
    Matt has worked 12 hours
    The total ammount of money owed to Matt is 72.
    The boss has given a bonus of 12 dollars.
    Name: Mat Bilcliff
    Rate: $5/hour
    Matt has worked hours.
    The total ammount of money owed to Matt is 0.
    The boss has given a bonus of dollars
    uuummm... It seems the bonus is out of order too. uuuummmm... I think it maybe a loop screwed up somewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More a math question than an algorithm
    By Gustaff in forum C Programming
    Replies: 1
    Last Post: 01-28-2003, 12:10 PM
  2. Stupid Math Question....really stupid
    By ToLazytoSignIn in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-16-2003, 06:36 PM
  3. Very large signed integer math question
    By Criz in forum C Programming
    Replies: 8
    Last Post: 12-01-2002, 11:43 AM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  5. Math Question
    By DarkEldar77 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2001, 12:52 PM
Website Security Test