Can't put a space between output and input on the same line - printf

After printing, if I write the name I get:
Insert a name :Andrea
There is no space before Andrea, even if I put the space in the output. How can I add a space before writing the name?
#include <stdio.h>
int main() {
int string[8];
printf("\nInsert a name : ");
printf(" ");
scanf("%s", string);
printf("\nThe name is : %s", string);
return 0;
}

The code that you provided does not compile because the variable string is an array of integers (and not a string as you might think). And when you input the value of it, you're telling the compiler to expect a string ("%s" in scanf("%s", string")).
Change your string declaration to char string[8] and it should work (with the spacing you want):
Note that the value that goes into string will not contain more than 8 characters.

Actually, you did a small mistake in your code, you declared the string to be an array of int & from the code scanf("%s", string") the compiler expects from the user to enter a string as it contains %s as the format specifier. Hence, the code doesn't compile.
In order to correct this, you should declare string as char string[8] (string containing 8 characters).
The correct code is given below :
#include <stdio.h>
int main() {
char string[8];
printf("\nInsert a name : ");
scanf("%s", string);
printf("\nThe name is : %s", string);
return 0;
}
From the above correction your problem for space between input and output will be solved!
Here, is the output which is received from this correction :
I hope so this explanation will be helpful for you!
If any problem persists then feel free to ask in comments! ;-)

Related

XOR reverse a string in objective-c get an error

I want to use the following code to reverse a char * type string in objective-c:
- (char *)reverseString:(char *)aString
{
unsigned long length = strlen(aString);
int end = length - 1;
int start = 0;
while (start < end) {
aString[start] ^= aString[end];
aString[end] ^= aString[start];
aString[start] ^= aString[end];
++start;
--end;
}
return aString;
}
But I got an error EXC_BAD_ACCESS at this line
aString[start] ^= aString[end]
I googled and found people said I can't modify a literal string because it is readonly. I am new to C so I wonder what simple data type (no object) I can use in this example? I get the same error when I use (char []) aString to replace (char *) aString.
I assume you're calling this like
[myObj reverseString:"foobar"];
The string "foobar" here is a constant literal string. Its type should be const char *, but because C is braindead, it's char *. But it's still constant, so any attempt to modify it is going to fail.
Declaring the method as taking char[] actually makes no difference whatsoever. When used as a parameter type, char[] is identical to char*.
You have two choices here. The first is to duplicate the string before passing it to the method. The second is to change the method to not modify its input string at all but instead to return a new string as output. Both can be accomplished using strdup(). Just remember that the string returned from strdup() will need to be free()'d later.

How to fix error Format specifies type 'char *' but the argument has type 'char'

I get a warning saying:
"Format specifies type 'char *' but the argument has type 'char'" for the student variable. I am copying/pasting the code out of a book into xcode and am not sure how to fix this. The only thing that prints in the console is "(lldb)". Any advice
#include <stdio.h>
void congratulateStudent(char student, char course, int numDays)
{
printf("%s has done as much %s Programming as I could fit into %d days.\n", student, course, numDays);
}
int main(int argc, const char * argv[])
{
// insert code here...
congratulateStudent("mark", "Cocoa", 5);
return 0;
}
void congratulateStudent(char *student, char *course, int numDays)
the %s means that you are going to print a string ( array of chars)
and char student this means that student is a char type
so student here is not a pointer to a string
In order to change the student type from char to a string pointer you have to add asterisk to student char *student
In your code you are calling the congratulateStudent with input parameter string "mark". So to support this string the input parameter student should be defined as pointer of string
so you are missing the asterisk in the definition of student
The same thing for course
void congratulateStudent(char *student, char *course, int numDays)
Use Function signature like because you are passing string as argument to function in main but function has character type argument..

How do I append blank spaces to the end of a string using printf?

How do I append blank spaces to the end of a string using printf?
I can't find any examples where spaces are appended to the right. A similar question I found use printf to add spaces to the left of the string instead.
Use negative numbers to left-align (i.e. "pad" to the right).
#include <stdio.h>
int main() {
const char *s = "hello";
printf("%30sworld\n", s);
printf("%-30sworld\n", s);
}
This prints
helloworld
hello world

Signed Char to Int

Basically, my problem is a signed char to int and string conversion in cocoa.
I found this piece of code in an open source cocoa bluetooth application and am trying to apply it to my own.
Basically, I get a signed char output from the variable "RSSI", and want to convert it to an int and a string, the string for outputting to the log and the int for further calculation. However, no matter what I try, I cannot seem to get it converted, and just get an EXEC_BAD_ACCESS if I try outputting the signed char to the log as it is.
A typical value for the signed char would be " -57 '\307' " quoted directly from the process before it is held up by the NSLog. Here's the code:
- (BOOL)isInRange {
BluetoothHCIRSSIValue RSSI = 127; /* Valid Range: -127 to +20 */
if (device) {
if (![device isConnected]) {
[device openConnection];
}
if ([device isConnected]) {
RSSI = [device rawRSSI];
[device closeConnection];
NSLog(RSSI);
}
}
return (RSSI >= -60 && RSSI <= 20);
}
Thanks in advance.
NSLog() takes an NSString format string as its first argument, and an (optional) variable length list of variables for the format specifiers in the format string after that:
NSLog(#"RSSI: %c", RSSI);
What you've got now (NSLog(RSSI);) is simply wrong. It should be giving you compiler warnings like these:
warning: passing argument 1 of 'NSLog' makes pointer from integer without a cast
warning: format not a string literal and no format arguments
You should always pay attention to compiler warnings, not ignore them. Especially when your program crashes on the same line the warnings refer to, they should be a red flag to you that you've made a mistake.
As an aside, I should mention that NSLog() works very much like printf(). The two major differences are that NSLog's format string should be an Objective-C string literal (#"string"), not a standard C char string ("string"), and that the format specifier for an object is %#. %# is replaced by the string returned by calling the -description method on the object to be printed.

loop on prompt with a yes or no?

Good afternoon,
I'm trying to accomplish a task that i know should be doable. however my attempts seem to fail every time. My endeavor is to learn to code in Objective -c and have been making good progress. what i would like to do is add a loop to my current application that asks at the end if i would like to run again or some thing to that regard, and reply with a yes or no. if no the program ends and if yes it jumps back to the top of the project to start all over. kinda like what i have below? forgive me please if its not quite perfect, im still getting used to programing and am finding it incredibly fun.
#include <stdio.h>
int main(void)
{
char loop = yes;
while (loop = yes)
{
.
.
.
}
printf ("would you like to continue (yes/no)/n");
scanf ("%s", loop);
}
The printf and scanf need to be moved up inside the curly braces of the while loop. Also, you want \n instead of /n in the printf. Finally, you're going to get a string back with that scanf() call, so you'll want to declare loop as a char array, and then in the while loop, check the first element of that array for a 'y' or 'n' or something like that. You might also want to look at getchar() instead of scanf() for that sort of thing.
Not compiled here, but should work:
#include <stdio.h>
int main(void)
{
char buffer[256];
do {
.
.
.
printf ("would you like to continue (yes/no)/n");
scanf ("%s", buffer);
} while (strcmp(buffer,"yes") != 0);
}
One wouldn't do anything like that in a real world application, but for demonstration purpose it should be ok.
I made your variable an array, because strings are arrays of characters in C. Length is set to 256 bytes (255 characters + 0-byte as delimiter). I changed the loop to do-while to make it run at least once. For string comparison you need to call a function. strcmp returns 0 for identical strings. Finally, the question belongs in the loop.
It is plain C though, using nothing of Objective-C.
int main() {
char A = 'n';
char B = 'y';
char Answer;
printf("Does the subject have a glazed over look? (y/n): \n");
scanf("%c",&Answer);
if (Answer=='N'||Answer=='y'|| Answer=='N'||Answer=='Y')
printf("Good\n");
else
printf("Please enter 'y' or 'n' \n ");
return 0;
}
#include <stdio.h>
int main(void)
{
avi;
char loop[10];
while (loop = yes)
{
.
.
.
}
printf ("would you like to continue (yes/no)/n");
scanf ("%s", loop);
if(strcpm(loop,"YES")==0) goto avi:
}