Jump to content

Print in screen numbers as inserted ????

- - - - -

  • Please log in to reply
4 replies to this topic

#1
mihosa

mihosa

    Newbie

  • Members
  • PipPip
  • 19 posts
Hello. I am trying to print some numbers in a c program as it is inserted in the program but i can't.

I think that i must convert the integer type to character for printing in the screen as it is but I can't find out how. Could anyone help me ???

This is my code:

struct sharedcosts{

    char surname[10];
    int floor[9];
    float millimetre[1000];
    int empty[1];
    float heating,elevator,othercosts;
}tenand[10];


        
        int main()
{
        struct sharedcosts instance;
        printf("Please insert your Surname      ");
        scanf("%s", & instance.surname);
        printf("Please insert your apartment floor      ");
        scanf("%d", & instance.orofos);
        printf("Please inster the millimetres of your apartment      ");
        scanf("%f", & instance.millimetre);
        printf("Please insert the number 1 if the apartment is empty otherwise insert the number 0      ");
        scanf("%d", & instance.empty);
        printf("Please insert the heating costs      ");
        scanf("%f", & instance.heating);
        printf("Please insert the elevator costs      ");
        scanf("%f", & instance.elevator);
        printf("Please insert the other costs of the  building     ");
        scanf("%f", & instance.othercosts);

        
        printf("********************************************************************************");
printf("Surname   Floor   Milimetres   State   Heating   Elevator   RestCosts");
        printf("********************************************************************************");
        printf("%s   ", instance.surname   );
        //printf("%d   ", instance.level   );
        //printf("%f   ", instance.milimetres   );
        //printf("%d  ", instance.state   );
        //printf(instance.heating   );
        //printf(instance.elevator   );
        //printf(instance.restcosts   );

    }

I make an example above to see what i mean:


Please insert your Surname:  Papadopoulos
Please insert your apartment floor:  3
Please inster the millimetres of your apartment: 90
Please insert the number 1 if the apartment is empty otherwise insert the number 0: 0
Please insert the heating costs: 0
Please insert the elevator costs: 20
Please insert the other costs of the  building: 50


After that the program is print out on the screen the following:

Surname             Floor     Milimetres   State    Heating   Elevator   RestCosts

Papadopoulos     1634172   6091135167057623220900000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00000  00   Press any key to continiue...


How can I make the program to print out the numbers that I am inserting to it as it is and not printing out the ASCII code of its characteer as I suppose that is doing ??



Thank you very much.

Edited by Alexander, 06 March 2011 - 09:55 PM.
Use code tags, # button.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The code you have provided was incorrect (and cannot run, orofos is not a member in your code), can you show us your full code that provides that result?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
mihosa

mihosa

    Newbie

  • Members
  • PipPip
  • 19 posts
Yes I am sorry. I just correct it.


Hello. I am trying to print some numbers in a c program as it is inserted in the program but i can't.

I think that i must convert the integer type to character for printing in the screen as it is but I can't find out how. Could anyone help me ???

This is my code:



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


struct sharedcosts{

char surname[10];
int floor[9];
float millimetre[1000];
int empty[1];
float heating,elevator,othercosts;
}tenand[10];



int main()
{
struct sharedcosts instance;
printf("Please insert your Surname ");
scanf("%s", & instance.surname);
printf("Please insert your apartment floor ");
scanf("%d", & instance.floor);
printf("Please inster the millimetres of your apartment ");
scanf("%f", & instance.millimetre);
printf("Please insert the number 1 if the apartment is empty otherwise insert the number 0 ");
scanf("%d", & instance.empty);
printf("Please insert the heating costs ");
scanf("%f", & instance.heating);
printf("Please insert the elevator costs ");
scanf("%f", & instance.elevator);
printf("Please insert the other costs of the building ");
scanf("%f", & instance.othercosts);


printf("********************************************************************************");
printf("Surname Floor Milimetres State Heating Elevator Rest Costs");
printf("****************************************** **************************************");
printf("%s ", &instance.surname);
printf("%d ", &instance.floor);
printf("%f ", &instance.millimetre);
printf("%d ", &instance.empty);
printf("%f ", &instance.heating);
printf("%f ", &instance.elevator);
printf("%f ", &instance.othercosts);

}




I make an example above to see what i mean:


Please insert your Surname: Papadopoulos
Please insert your apartment floor: 3
Please inster the millimetres of your apartment: 90
Please insert the number 1 if the apartment is empty otherwise insert the number 0: 0
Please insert the heating costs: 0
Please insert the elevator costs: 20
Please insert the other costs of the building: 50


After that the program is print out on the screen the following:

Surname Floor Milimetres State Heating Elevator RestCosts

Papadopoulos 1634172 6091135167057623220900000000000000000000000000
00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000.00000 00 Press any key to continiue...



How can I make the program to print out the numbers that I am inserting to it as it is and not printing out the ASCII code of its characteer as I suppose that is doing ??



Thank you very much.

#4
wixifo

wixifo

    Newbie

  • Members
  • PipPip
  • 13 posts
Here's a working code

#include<stdio.h>


struct sharedcosts{

	char surname[10];

	int floor, empty;

	float heating, elevator, othercosts, millimetre;

} instance;



int main()

{

	printf("Please insert your Surname : ");

	scanf("%s", & instance.surname);

	printf("Please insert your apartment floor : ");

	scanf("%d", & instance.floor);

	printf("Please inster the millimetres of your apartment : ");

	scanf("%f", & instance.millimetre);

	printf("Please insert the number 1 if the apartment is empty otherwise insert the number 0 : ");

	scanf("%d", & instance.empty);

	printf("Please insert the heating costs : ");

	scanf("%f", & instance.heating);

	printf("Please insert the elevator costs : ");

	scanf("%f", & instance.elevator);

	printf("Please insert the other costs of the building : ");

	scanf("%f", & instance.othercosts);



	printf("****************************************** **************************************\n");

	printf("Surname Floor Milimetres State Heating Elevator Rest Costs\n");

	printf("****************************************** **************************************\n");

	printf("Surname : %s \n", instance.surname);

	printf("Floor : %d \n", instance.floor);

	printf("Millimetre : %f \n", instance.millimetre);

	printf("Empty : %d \n", instance.empty);

	printf("Heating : %f \n", instance.heating);

	printf("Elevator : %f \n", instance.elevator);

	printf("Other costs : %f \n", instance.othercosts);


	return 0;

}



Your biggest problem was there :


printf("%s ", &instance.surname);

printf("%d ", &instance.floor);

printf("%f ", &instance.millimetre);

printf("%d ", &instance.empty);

printf("%f ", &instance.heating);

printf("%f ", &instance.elevator);

printf("%f ", &instance.othercosts);


The & sign means the location in memory, remove it and you get the value instead.

Notice that when we use a float there are decimal numbers and when we use a int there are not. The [10] is the length only for char types, because a char is a single character, we declare an array of 10 characters to hold a "string". It does not work the same way for numbers.

#5
mihosa

mihosa

    Newbie

  • Members
  • PipPip
  • 19 posts
I correct it and is fine now :-)

Thank you very much !!!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users