Thread: simple char question

  1. #1

    simple char question

    How would you define a string so that it is just the size of the user input??

    right now in my calc program i have

    Code:
    int main()
    {
     char calc[50];
     cout<<"Calc: ";
     cin>>calc;
     for(int i=0;i<50;i++)
     {
      cout<<calc[i];
     }
     getchar();
     return 0;
    }
    but ofcourse as soon as there are empty spaces it starts displaying weird characters which i don't want it to.

    I just started this but its supposed to become a calculator which accepts inputs such as: 3*3+8

    Original Idea coming from: joshdick


    -Devouring One-
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You should just zero out the contents when you declare it. The best way to do it is as follows:

    Code:
    char buffer[256] = { 0 };
    That will set every element to 0 and you won't get the garbage stuff.

  3. #3
    thanks it works
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  4. #4
    Is there anyway to check if a character is a certain type??

    In my case I would want it to check if it were *,/,-,+ any of these.

    I know there is ctype.h but it hasn't helped me much
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Yeah its not a big deal. * / + - , those are all just characters right? So you can do an easy check.

    Code:
    if( calc[ i ] == '*' )
    {
      // Do something creative
    }
    Assuming i is any valid range in your array. Don't attempt to compare strings like this but it will work for single characters.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Arg... something is wrong but i can't figure out what

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
     char num[20]= { 0 };
     double temp = 0; 
     cout<<"Calc: ";
     cin>>num;
     for(int i=0;i<50;i++)
     {
       
       if(num[i] == '*')     
       {
        temp = (num[i-1])*(num[i+1]);
       }
       if(num[i] == '/')
       {
        temp = (num[i-1])/(num[i+1]);
       }
       if(num[i] == '+')
       {
        temp = (num[i-1])+(num[i+1]);
       }
       if(num[i] == '-')
       {
        temp = (num[i-1])-(num[i+1]);
       }
     }
     
     cout<<"\n"<<temp;
     getchar();
     return 0;
    }
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Be very careful with arrays!!

    Look at your loop with i = 0.

    Code:
    if(num[i] == '*')    
    {
      temp = (num[i-1])*(num[i+1]);
    }
    num[i-1] thats not in the bounds of the array!! You need to make sure you don't do this sort of thing.

  8. #8
    Ok even if i make as simple as only being able to do stuff like 3+3
    it gives me answers like 94 and higher

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
     char num[20]= { 0 };
     double temp = 0; 
     cout<<"Calc: ";
     cin>>num;
     for(int i=0;i<50;i++)
     {
       
       if(num[i] == '*')     
       {
        temp = (num[0])*(num[2]);
       }
       if(num[i] == '/')
       {
        temp = (num[0])/(num[2]);
       }
       if(num[i] == '+')
       {
        temp = (num[0])+(num[2]);
       }
       if(num[i] == '-')
       {
        temp = (num[0])-(num[2]);
       }
     }
     
     cout<<"\n"<<temp;
     getchar();
     return 0;
    }
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  9. #9
    i woked on it some more and now i have something like this which makes sence but it doesn't work:

    Code:
    if(num[i] == '-')
       {
        x = i-1;
        y = i+1;
        temp = (num[x])-(num[y]);
       }
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay a few things then I'm going to sleep. PM me and I can get back to you tomorrow if you have more questions.

    Now look at your for loop, you still go to 50 but you only have 20 elements! Change that down to 20. Also, you are adding the ascii values of the characters '3' and '3' right now. The ascii value of '3' is 51. This is obviously not what we want here. We need to convert it to floating point. We can use atof found in the stdlib.h to do this. So our if statement will look like this:

    Code:
    if(num[i] == '+')
    {
      temp = atof(&num[0]) + atof(&num[2]);
    }
    Note I am using &num[0], need to pass the address of it into the function because it requires a pointer to a character string. If you want to see more about the function prototype or anything you can look on msdn.

    msdn.microsoft.com

    Good Luck.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Simple Proxy
    By Lina in forum C Programming
    Replies: 0
    Last Post: 04-01-2007, 12:36 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. question about functions and char *
    By Bittrexx in forum C Programming
    Replies: 4
    Last Post: 07-22-2003, 12:27 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM