How to strcopy a single character from a string

This is a discussion on How to strcopy a single character from a string within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a simple calculation program, that returns the mathmatical sequence entered written out with the total. ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    4

    How to strcopy a single character from a string

    I am trying to write a simple calculation program, that returns the mathmatical sequence entered written out with the total.

    Code:
    //still doesnt add the number to the string "total" as it goes through the string
    
    #include <math.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <iostream.h>
    	void test8();
    	void test8() {//Input of mathmatical sequence and output 
    	//of the eqn in text
    
    	char s[256]; int i; int r=0; char *p=s; char c;
    	char total[]={" "};
    
    	cout<<"Please enter the mathmatical statement"<<endl;
    	cout<<"Include the = sign at the end of the statement"<<endl;
    	//could use a search and strcopy fucn to add = sign if it wasnt entered
    	cin>>s;
    	
    	
    	while (isdigit(*p)) r=r*10+(*p++)-'0';
    	do {
    	 c=*p++;
    	 if (c =='=') strcat(total," equals ");
    	  if (c =='=') break;
    	
    		if (isdigit (*p)) strcat(total,p);
    		
    	 i=0;	
    	 while (isdigit(*p)) i=i*10+(*p++)-'0';
    	 switch (c) {
    	 	case '+': r += i;strcat(total," plus "); break;
    	 	case '-': r -= i;strcat(total," minus "); break;
    	 	case '*': r *= i;strcat(total," times "); break;
    	 	case '/': r /= i;strcat(total," divided by "); break;
    
    	 	default: cout<<"Bad input"<<endl;
    	 }		//this does the computation in the order it was typed
    	 //no preference is given for * and / over + and -
    	}	while (true);
    		
    	cout <<total<<r<<endl;
    }
    int main() {test8(); return 0;}
    I realize there are one or two things i still need to touch up here, but my problem is how to add the numberical value in the string s to the string total. The line if (isdigit (*p)) strcat(total,p); is not working. I have tried variation on this, and tried to include it in the switch function, but to no avail. I would appreciate any suggestions on how to go about this.

    Thanks

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i'll try to help. can you give me a little more info as to what math statment might be inputed.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    Something simple, like 5+2*6-3=

    I realize that the program does not give preference to multiplication and division, but that is needed at this point. But I want to be able to out put like this for the previous input:

    5 plus 2 times 6 minus 3 equals 39

  4. #4
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    ok, what's this for? the easiest would to have multiple cins, is that possible?

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I'm not sure I'm clear on this... Is your question, how do you take a string and add it to a number?

    If so you need to work with numbers (i.e int's) and then generate
    your string later based on this, if you want your output as strings.
    Last edited by Azuth; 04-18-2002 at 01:21 AM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    How about?

    if (isdigit (*p)) strncat(total,p,1);

  7. #7
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i think he wants both the string and the clac, how would you do that? you can static cast right?

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    4
    I need the input to be a sting I think, to be able to accept the mathmatical signs as well.

    I tried adding cases to the switch command, for numbers 0-9, e.g.

    case '0': strcat(total," 0 "); break;

    but this did not work, nothing was added to the string. I really think the command needs to go in the section with the switch statement, so that it builds the string "total" in order, but I am not positive. The main thing right now is getting it to recognize the digit characters in string "s" and adding them to the string "total"....I have not been able to figure out how to accomplish this

  9. #9
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    OK. So I would imagine you need to read your string, tokenise it, convert the number strings to int's, use the functional operators in a set of if statements along the lines of ;

    if (operator1=="*") then{
    total = int1 * int2;
    }
    if (operator1=="+") then{
    total = int1 + int2;
    }

    Then output your total. There are a number of simple C calculators out there, you may want to look at them for inspiration.

    Of course it's possible I have the wrong end of the stick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. How to read single characters in a string
    By MaxxMan-X in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2005, 01:52 PM
  4. Printing single character from a string
    By TJJ in forum C Programming
    Replies: 4
    Last Post: 11-05-2003, 05:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21