C++ :: Why Two Scanf Needed To Read Character
Mar 11, 2012
Below given is the code, which allocates memory for a structure dynamically and stores value in its member. The problem is in the last scanf statement which reads 'ch'. The code will be in infinite loop as it doesnt executes the last scanf statement. The solution for this is (i use this) to add one more similar scanf statement for 'ch' in the very next line. If i do so, it executes the statement, reads 'ch' and then continues.
I want to know why it behaves like that..
struct student{
int usn;
};
int main(){
char ch;
struct student *s;
s=(struct student *)malloc(sizeof(struct student));
[Code] ....
View 11 Replies
ADVERTISEMENT
Nov 1, 2013
So running the following code
Code:
#include <stdio.h>
int main(void) {
char c;
int i=1;
while (scanf("%c", &c)==1)
printf("loop sequence %i: %c(%i)
[Code] ......
Done it seems a "carriage return" serves two purposes here, one is to signal the program to read in the character typed in before the "carriage return", another serves as a second character typed, how can i do this cleanly, that is without having to use a "carriage return" as the second character to signal "I've typed in the first character already".
View 2 Replies
View Related
Dec 26, 2014
I know that the null at the end of the string indicates end of that string but why is it actually needed. Strings in C are just arrays of char variables. A "Hello" string would be stored in ascii code as:
char string[] = {72, 101, 108, 108, 111, 0}
When I have array of integers like
int intArray[] = {72, 101, 108, 108, 111}
I can perform various operations with it like comparision with other array etc. but I don't need terminating character
View 6 Replies
View Related
Oct 5, 2014
I'm trying to make a character array, and be able to custom fill it by using the scanf() function to ask the user for each character spot in the array.
Code:
#include <stdio.h>
int main() {
int i;
char character_array[11];
char *charpointer;
charpointer = character_array;
[Code] .....
And this is the output i get when i run it:
Code:
root@kali-Tulips:~# ./myown
what letter would you like in the [0] spot of the array? :
a
what letter would you like in the [1] spot of the array? :
what letter would you like in the [2] spot of the array? :
b
what letter would you like in the [3] spot of the array? :
what letter would you like in the [4] spot of the array? :
[Code] .....
Obviously I'm trying to grasp C programming and am very new. I don't understand why this isn't working, and i think its more than my lack of knowledge of C syntax. I believe the correct memory is allocated but when examined with gdb i don't find what i expect....
View 4 Replies
View Related
Nov 10, 2014
I'm trying to write a code that is read character user 'e' or ' ' space also numbers I mean a number 'e' or space 'e' a number 'e' or space so forth.But i get absurd numbers. The program shows me the added number. If '
' entered the taking numbers will stop(scanf will stop).
Example input:
e 1 8 7 2 3 6
or e 1 e 8 e 7 e 2 e 3 e 6
Code: #include <stdio.h>
#define MAX 10
void addq ( int *, int, int *, int * ) ;
void test();
[Code]....
View 2 Replies
View Related
Nov 10, 2013
I have to optimize a code for below scenario. I am reading stdin (a file redirected to stdin) character by character. How many chars are going to come is not known. After every few chars there is a seaparator. e.g $ as below
rhhrkkj$hghjhdf$ddfkrjt
While reading, if the separator arrives I'm processing the string stored before that separator and then continue reading stdin in same fashion, till EOF. I am using getc(stdin) to read chars.
Using gprof I can see most of the program time is spent inside main() , for this reading logic. Rest of the program is just some insert and search operations. I am getting time of 0.01 secs at the moment, want to reduce further.
View 6 Replies
View Related
Aug 10, 2012
How do I write an a program that will read an input file character by character?
View 1 Replies
View Related
Oct 6, 2013
In my program, I'm supposed to read a text file (the name of which is given to me as a command line paramater, as long with an integer), and display the text in a specific format (each line can only be as long as the integer). However, I'm having trouble even reading the text file. I don't know the syntax. I'm only allowed to edit the function that does the formatting, and the code in that is
void typeset (int maxWidth, istream& documentIn)
I don't know how to 'read' the file, as most examples online are ifstream, or openFile or something like that. What I want to do is just read the first character of the file, and continuously keep reading characters until the end of the file.
View 10 Replies
View Related
Feb 14, 2014
One thing make me consider:
FistClass F1 = new FistClass ();
FistClass F2 = F1;
second line why i dont need to firstly create instance for F2 and then make reference betwwen to classes like:
FistClass F1 = new FistClass ();
FistClass F2 = new FistClass ();
F2 = F1;
View 9 Replies
View Related
Feb 13, 2014
how to carry out the conversions. The assignment is the normal hex to octal and Quart (base 4) via bit munipulation which I have worked out myself. However, I have been trying all day to figure out how to read in a string such as H1234, or O4567. How to parse the input I can handle the remainder myself. I'm just stuck and I've tried for hours.
View 5 Replies
View Related
Sep 25, 2012
I am working to make a translating software from an Urdu sentence into Hindi and vice versa, i am using visual c++ 2010 software with c++ language. i have written an Urdu sentence in a text file. now i want to get a single character one by one from that file so that i can work on it to convert it into its equivalent Hindi character. when i use get() function to get a single character from input file and write this single character on output file, i get some unknown ugly looking character placed in output file. My code is as follows
Code:
#include<iostream>
#include<fstream>
#include<cwchar>
#include<cstdlib>
using namespace std;
void main() {
wchar_t arry[50];
[Code] .....
View 13 Replies
View Related
Mar 5, 2013
Write a C++ program to calculate the fewest number of each denomination needed to pay a bill of amount TOTAL. For example, if the end user enters $97 for TOTAL, program will output that the bills would consist of one $50 bill, two $20 bills, one $5 bill, and two $1 bills. (Assume that the amount is in whole dollars, no cents, and only $100, $50, $20, $10, $5, and $1 denominations are available.) Aid: You may want to use the modulus operator %.
View 1 Replies
View Related
Jan 10, 2014
How to do this?
View 10 Replies
View Related
Jan 12, 2015
I'm very new to c programming and I have some background in C# and java. I am supposed to read a string from input then determine in that string, which character is the largest, i.e. I think b>e and e>z, e.t.c. If the string is empty I should return '