Thread: concatenating variables

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Unhappy concatenating variables

    How do I combine variables into one variable?
    I'm making a small calculator program and don't know how to combine the first number, the second number and the opperator into an answer variable

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by who_cares
    How do I combine variables into one variable?
    I'm making a small calculator program and don't know how to combine the first number, the second number and the opperator into an answer variable
    well u cant store all these three thing in one single varibale but u can store all these thing in one single string or an array

    to store that in an array
    Code:
    arr[0] = val1;
    arr[1] = op;
    arr[3] = val2;
    to store that i a string

    NOTE as each element of an string is one byte the val should have one digit number
    Code:
    str[0] = val1; // note val1 should contain one digit not more than that, if u want to store more than one digit its better u go for array ones
    str[1] = op;
    str[2] = val2
    ssharish2005

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    well, I'm not using but one string. And it is only one char
    I have this written: it works untill the line where the answer is declared. I need to combine num_1, opp, and num_2 isto a math problem that will get solved.
    Code:
    /*
    Name: scanf test
    
    Author: who_cares
    
    Version 0.0.4
    
    Use: Small calcualtor script, I hope. I don't know much about scanf, or C in general
    */
    #include <stdio.h>
    
    int main()
    {
    	//
    	//vaiables
    	//
    	int num_1, num_2, answer;
    	char opp;
    	
    	//Number 1
    	printf("Enter the first number:");
    	scanf("%d", &num_1);
    	//Opperator (+, -, *, /)
    	printf("Enter your operator:");
    	scanf(" %c", &opp);
    	//Second number
    	printf("Enter the second number:");
    	scanf("%d", &num_2);
    	//Put it all together
    	answer = num_1 opp num_2;
    	//Print it all out
    	printf("%d %c %d = %d\n", num_1, opp, num_2, answer);
    }

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This:
    Code:
       answer = num_1 opp num_2;
    should be done something like this:
    Code:
       switch ( opp )
       {
       case '+':
          answer = num_1 + num_2;
          break;
       case '-':
          answer = num_1 - num_2;
          break;
       case '*':
          answer = num_1 * num_2;
          break;
       case '/':
          if ( num_2 )
          {
             answer = num_1 / num_2;
          }
          break;
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    answer = num_1 opp num_2;
    u cant do that, its an error. instead u can choose a better way by using if else statement or and switch case statment which work more effecient for u'r case.

    ssharish2005

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    okay, I'll have to use a switch then.
    I haven't really learned those yet... but I know they're in my book

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Replies: 5
    Last Post: 09-05-2002, 10:16 AM