Thread: putting integer in string

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Quote Originally Posted by master5001 View Post
    Snap! Its more satisfying to notice that on your own, I am willing to bet Keep up the good coding, friend.
    Thanks. I always find myself writing a program, then getting tired and frustrated, missing stupid errors, soI go back to it with a fresh mind, solve the problem, and then just wanna keep adding and adding more and more to my program, and the cycle begins again.

    BTW, my program is making realy good progress (In large part due to the excellent helpers here!) and I almost have it finished. When I do, I want to make a GUI for it, and post it on my website, so I'll keep y'all posted.

    There are a few ways to solve this problem. Simplest is to set it to '\0' and then check if it's '\0' - in that case, don't print it [putting a '\0' there will make string zero-length, becasuse sprintf isn't clever enough to know that you don't want a '\0' at the beginning of the string and thus make it an empty string].

    The second option is to use a char array instead of a single char. An empty string "" will print as "nothing" in sprintf.

    The third option is to use different format strings - only put the %c in the format string if it's actually needed - otherwise it will be ignored. Just beware that this ONLY works when it's the last argument(s) of sprintf that you want to skip, there is no way to skip argument 2, but print args 1, 3 and 4. You just have to use different printfs to do that.

    --
    Mats
    Thanks, number two sounds like the best option for me, in fact this is exactly what I want, cheers.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by bertazoid View Post
    Thanks. I always find myself writing a program, then getting tired and frustrated, missing stupid errors, soI go back to it with a fresh mind, solve the problem, and then just wanna keep adding and adding more and more to my program, and the cycle begins again.
    That is basically how the game is played my friend. They say absense makes the heart grow fonder. When it comes to programming it makes the brain grow more observant and objective.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    very true

    matsp: I tried using a string instead of a char within my switch statement, but couldn't get it to work. When I declared it outside the switch statement, and then initialized it within, I got the compiler error: "Incompatible types in assignment." When I tried to declare the string within the statement, I got the compiler error "String undeclared..." (pointing meto the line wheren I'm trying to print it. I tried testing this idea in a program of its own, within an if statement, and got the same results. Here's the two codes I used respectively:

    Code:
    #include <stdio.h>
    
    int main () {
    
    int n=1;
    if(n==1){
      char string[]="+";
    }
    else{
      char string[]="";
    }
    
    printf("%s", string);
    
    return 0;
    }
    Code:
    #include <stdio.h>
    
    int main () {
    
    int n=1;
    char string[5];
    
    if(n==1){
      string="+";
    }
    else{
      string="";
    }
    
    printf("%s", string);
    
    return 0;
    }
    I don't understand why this doesn't work, since declaring/initializing other variable types within if/select statements seems fine.

    Also I noticed with the original case, where I was declaring a char within the statement (On one of the three conditions), then used sprintf, I wasn't getting the hexagonal question mark at the front of the string, when I used this by itself in int main, but for some reason I do get it when I use it within a function of its own (with a string as an input parameter), and then call the fucntion in main....The function is of type int and returns 0, but it does apply this process to the input string - just wrongly so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Putting integer in a string
    By minesweeper in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2003, 12:43 PM