How do I get a string to output a letter instead of a vaule in borland???
Printable View
How do I get a string to output a letter instead of a vaule in borland???
what?
Code:char var = 'A';
printf("%c", var); //not d
//or
int i;
char string[] = { "How far to Andromeda?" };
stringLength = strlen(string);
for( i = 0; i < stringLength + 1; i++)
printf("%c", string[i]);
thanks Adak
so if I wanted to create a program that outputted a letter that the user inputted, would it look like this:
if that is true say I want the user to input a speific letter, would it look like this:Code:printf ("Please input letter: ");
int %c
printf ("The letter you have inputted is: "%c);
Code:printf ("Please input letter: ");
int X=A
int X=B
if X=A printf ("The letter you have inputted is A")
if X=a printf ("Please input letter in captials")
if X=B printf ("The letter you have inputted is A")
if X=b printf ("Please input letter in captials")
So... if you want to create a program that output a letter that the user inputted you can do smthg like this:
You can do this in many other ways.Code:char letter;
printf ("Please input letter:\n");
do {
letter = getch();
printf ("\nThe letter you have inputted is: ");
putch(letter);
}
while (letter != 27);
geekrockergal, you need to hit the books or tutorials to understand the basic syntax of C before proceeding.
I see. Well, you have a lot to learn still.
Good luck with your studies then.
so is it possiable to do this (I want the user to input a speific character A or B)
Im sorry people I really am trying my hardest to get my head around thisCode:char letter;
printf ("Please input letter:\n");
if {
letter = getch(A);
printf ("\nThe letter you have inputted is: A ");
putch(letter);
}
else{
letter = getch(a);
printf ("\nPlease input a captial A ");
putch(letter);
}
if {
letter = getch(B);
printf ("\nThe letter you have inputted is: B ");
putch(letter);
}
else{
letter = getch(b);
printf ("\nPlease input a captial B ");
putch(letter);
}
If WHAT? If you have an if, then you have then what, too, no?Code:if {
getch takes 0 arguments.
If u want the user to imput a specific char you can do smthg like:
You could try to do this also by using scanf function and a switch.Code:int letter;
printf ("Please input letter [A or B]:\n");
do {
letter = getch();
letter = toupper(letter);
}
while ((letter != 'A') && (letter != 'B'));
if (letter == 'A')
printf("The letter you have inputted was: A\n");
else
printf("The letter you have inputted was: B");
thanks again
is it possiable to have multiple else statements:
Code:int letter;
printf ("Please input letter [A, B, C]:\n");
do {
letter = getch();
letter = toupper(letter);
}
while ((letter != 'A') && (letter != 'B') && (letter != 'C'));
if (letter == 'A')
printf("The letter you have inputted was: A\n");
else
printf("The letter you have inputted was: B");
else
printf("The letter you have inputted was: C");
Yes, it is. But again, you need a condition.
The if statement is like
"if x do this, if y do this, if z do this, else do this."
This is the essence of the if.
if (condition)
else if (condition)
else if (condition)
else
again thank you Im at last understanding !!!!!!!
I did what you said and Im getting two errors and two warnings
error 22: do - while state missing ;Code:/*********************
Program Name:Program 1
Author:geekrockergal
*********************/
/*********PREPREOCESSOR DIRECTIVES*************/
/****************HEADER FILES******************/
#include <stdio.h>/*Standard I/O Header*/
#include <conio.h>/*Console I/O Header*/
/************NAMED CONSTANTS*******************/
/****USER-DEFINES AND ENUMERATED DATA TYPES****/
/************FUNCTION PROTOTYPES***************/
/*************GLOBAL VARIABLES*****************/
/***************MAIN FUNCTION******************/
int main (void)
{
int letter;
printf ("Please input letter[A, B, C or D]: \n");
do {
letter = getch();
letter = toupper(letter);
}
while (letter != 'A') && (letter != 'B') && letter != 'C') && (letter != 'D'));
if(letter == 'A')
printf("The letter inputted was A\n");
else if(letter == 'B')
printf("The letter inputted was B\n");
else if(letter == 'C')
printf("The letter inputted was C\n");
else(letter == 'D')
printf("The letter inputted was D\n");
}
/**************OTHER FUNCTIONS*****************/
error 30: statement missing ;
warning 30: code has no effect
warning 31: function should return vaule
Im bet you lot are thinking just quit while your ahead
=( =( =( =(
error 22: do - while state missing ;
you missed the ( after while
else(letter == 'D')
or use else if (codition)
or just else
there is no
else (condition)
possibility in C
I had to add the corrections in the syntax you must make in red.Code:/*
Program Name:Program 1
Author:geekrockergal
*/
/* PREPREOCESSOR DIRECTIVES */
/* HEADER FILES */
#include <stdio.h>
#include <conio.h>
/* NAMED CONSTANTS */
/* USER-DEFINES AND ENUMERATED DATA TYPES */
/* FUNCTION PROTOTYPES */
/* GLOBAL VARIABLES */
/* MAIN FUNCTION */
int main()
{
int letter;
printf("Please input letter[A, B, C or D]: \n");
do
{
letter = getch();
letter = toupper(letter);
}
while ( (letter != 'A') && (letter != 'B') && (letter != 'C') && (letter != 'D') );
if (letter == 'A')
printf("The letter inputted was A\n");
else if (letter == 'B')
printf("The letter inputted was B\n");
else if (letter == 'C')
printf("The letter inputted was C\n");
else /*(letter == 'D')*/
printf("The letter inputted was D\n");
}
/* OTHER FUNCTIONS */
The last, I just removed everything from the else by putting it inside comments.
"else" means just that - everything else. Everything that does not fit the conditions listed above.
And don't forget indentation while you're at it.
I suspect this might be an exercise on if, but you can also write it as:
Code:/*
Program Name:Program 1
Author:geekrockergal
*/
/* PREPREOCESSOR DIRECTIVES */
/* HEADER FILES */
#include <stdio.h>
#include <conio.h>
/* NAMED CONSTANTS */
/* USER-DEFINES AND ENUMERATED DATA TYPES */
/* FUNCTION PROTOTYPES */
/* GLOBAL VARIABLES */
/* MAIN FUNCTION */
int main()
{
int letter;
printf("Please input letter[A, B, C or D]: \n");
do
{
letter = getch();
letter = toupper(letter);
}
while ( (letter != 'A') && (letter != 'B') && (letter != 'C') && (letter != 'D') );
printf("The letter inputted was %c\n", letter);
}
/* OTHER FUNCTIONS */