Jump to content

How to remove last comma

TheRedViper

Hello guys, there's a loop in my program that prints the odd numbers within an interval with a comma in-between each result. However, the way I programmed it, it prints a comma after the last result and I don't want that. Is there a way to modify or create another loop to remove the last comma? Commentary is in french but you don't need it to understand the program (if you do, simply ask).

 

#include <stdio.h>
#include <stdlib.h>

void main()
{
	int iInterval1 = 0, iInterval2 = 0, i, iChoix1 = 0; 
	
		
		do
		{
			system("cls");
			//Entrer des données par l'utilisateur
			printf("Entrez l'intervalle: ");
			scanf("%i %i", &iInterval1, &iInterval2);
			printf("\n");
				
				//Affichage des nombres impairs entre Interval1 et Interval2
				printf("Les nombres impairs entre %i et %i sont: ", iInterval1, iInterval2);
				//Création de l'intervalle selon les deux choix de l'utilisateur
				for (i = iInterval1; i <= iInterval2; i++)
				{	
					//Vérification par modulo si les nombres sont impairs puis affichage
					if (i % 2 != 0)
					{
						printf("%d,", i);
					}
				}
			//Espacement
			printf("\n\n");	
			//Quitter ou recommencer le programme
			printf("Voulez-vous recommender? (1 = oui, 2 = non)");
			scanf("%d", &iChoix1);

		}
		//si l'utilisateur appuit sur 1 le programme recommence
		while (iChoix1 == 1);
		
}

 

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
Share on other sites

Link to post
Share on other sites

you could consider printing the first number outside the loop, and then in the loop, coma and the next number.

 

also PLEASE use code tags thanks

Edited by espurritado

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

I took @espurritado approach but I have made that last number is not printed inside the loop, so I have changed condition.

 

I made also example values for iInterval1 and iInterval2.

 

#include <iostream>

int main() {
  int iInterval1 = 10;
  int iInterval2 = 16;
  
  int iInterval2Temp = iInterval2;
  if(iInterval2 % 2 == 0){
    iInterval2Temp -= 3;
  } else {
    iInterval2Temp -= 1;
  }
  
  for (int i = iInterval1; i <= iInterval2Temp; i++)
  {    
    if (i % 2 != 0)
    {
      printf("%d,", i);
    }
  }
  
  if(iInterval2 > iInterval1){
    if(iInterval2 % 2 == 0){
      printf("%d", iInterval2 - 1);
    } else {
      printf("%d", iInterval2);
    }
  }
}

So before a loop I do check iInterval2 and create iInterval2Temp variable that I will use for loop condition. I keep original iInterval2 so I can check it after the loop and decide how to print last number, or do I even should print it, if iInterval2 is less or equal iInterval1 then no number will be printed.

 

You could make an additional condition inside loop:

#include <iostream>

int main() {
  int iInterval1 = 10;
  int iInterval2 = 16;
  
  for (int i = iInterval1; i <= iInterval2; i++)
  {    
    if (i % 2 != 0)
    {
      if((iInterval2 % 2 == 1 && iInterval2 == i) || iInterval2 - 1 == i){
        printf("%d", i);
      } else {
        printf("%d,", i);
      }
    }
  }
}

But you would need to check that condition as many times as loop goes, and excluding one numer makes you have only two additional conditions constant.

Link to comment
Share on other sites

Link to post
Share on other sites

40 minutes ago, Mr_KoKa said:

I took @espurritado approach but I have made that last number is not printed inside the loop, so I have changed condition.

 

I made also example values for iInterval1 and iInterval2.

 


#include <iostream>

int main() {
  int iInterval1 = 10;
  int iInterval2 = 16;
  
  int iInterval2Temp = iInterval2;
  if(iInterval2 % 2 == 0){
    iInterval2Temp -= 3;
  } else {
    iInterval2Temp -= 1;
  }
  
  for (int i = iInterval1; i <= iInterval2Temp; i++)
  {    
    if (i % 2 != 0)
    {
      printf("%d,", i);
    }
  }
  
  if(iInterval2 > iInterval1){
    if(iInterval2 % 2 == 0){
      printf("%d", iInterval2 - 1);
    } else {
      printf("%d", iInterval2);
    }
  }
}

So before a loop I do check iInterval2 and create iInterval2Temp variable that I will use for loop condition. I keep original iInterval2 so I can check it after the loop and decide how to print last number, or do I even should print it, if iInterval2 is less or equal iInterval1 then no number will be printed.

 

You could make an additional condition inside loop:


#include <iostream>

int main() {
  int iInterval1 = 10;
  int iInterval2 = 16;
  
  for (int i = iInterval1; i <= iInterval2; i++)
  {    
    if (i % 2 != 0)
    {
      if((iInterval2 % 2 == 1 && iInterval2 == i) || iInterval2 - 1 == i){
        printf("%d", i);
      } else {
        printf("%d,", i);
      }
    }
  }
}

But you would need to check that condition as many times as loop goes, and excluding one numer makes you have only two additional conditions constant.

Thank you, that helped me a lot.

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×