C :: Get Rid Of Space / Use Scanf Or Getchar

Sep 24, 2014

I want the user to be able to enter a command then a character, like for example: push r....I want the command to be stored in the array command, and the character to be stored in the variable c.

Now I wonder what the best way to get rid of the space is, using scanf or getchar (see below for code, only thing that I changed between the 2 versions is the statement before the comment "get rid of space")? Or maybe it doesnt matter?

Code:

include <stdio.h>
#define MAX 200
void push(char c); // Puts a new element last in queue
char pop(void); // Gets the first element in queue
static char s[MAX];
}

[code]....

View 6 Replies


ADVERTISEMENT

C :: Getchar - Clear Input Buffer For Next Input Through Scanf

Aug 13, 2014

I would just like to know what does

while( (c =getchar())!='
' && c !=EOF );

do ? and how it do it?

I have see it in a example to clear the input buffer for next input through scanf but does not know its working clearly.

it is used in this example :

Code:
#include<stdio.h>
struct linklist {
char value[20];
struct linklist *next;
};
struct linklist *head,*curr;

[Code] .....

View 4 Replies View Related

C/C++ :: Checking For A Space In Scanf

Jan 23, 2014

I have created a prompt which prompts the user for an integer and I have set up a loop to check for if it is an integer or not. My "bug" is that a user can enter an "integer" and "space" and "enter" and it does not give any error and assumes that "All is FINE!". I have gotten the value from the ascii table of 'SPACE' and put it as a check in my parameter of while, but it does not work.

Here is my code:

int x, y, boolean, i;
char buff[256];
printf("Enter the first integer value: ");
scanf("%s", buff);
i = 0;
boolean = 0; //initializing our boolean var that will eventually decide if we have an error or not

[code]....

View 4 Replies View Related

C :: Swap Chars Using Getchar

Sep 23, 2014

This is for homework . Must use only getchar and putchar

Code:

int main(void) {
int pch; //first
int ch; //second

[Code]....

And it works , but i need to hit ENTER two times when i have 3,5,7... chars to print result.

View 6 Replies View Related

C :: Getting Integer With Only Using Getchar Function

Nov 16, 2013

I need to get an integer number with only using getchar() and without pre knowing the number of digits in the integer. And I cannot use strings or arrays.

View 4 Replies View Related

C/C++ :: Calculator That Uses Only Getchar / Putchar

Mar 24, 2014

I basically just have to make a calculator that only uses getchar/putchar (printf is Ok for displaying errors) - Also, negatives must be error-checked. As of right now, my program will only work if there is exactly one space between the number, operand, and other number. No spaces, or more than one space, will give me some very weird digits. I also need to display the numbers with putchar, and not printf (as I did below), but I just really wanted a working program.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int add(int input1,char operand, int input2);
int subtract(int input1,char operand, int input2);

[Code] .....

View 12 Replies View Related

C++ :: Getchar - Return Assigned Value

Nov 9, 2012

Once again i hit a very simple problem i am unable to resolve. I using Visual Studio 2010, but am compiling for C.

This:

Code:
char i=45;
while(i=getchar() != EOF)
{

should imo work perfectly (yes, no real code, just to demonstrate the issue), but it doesnt. Getchar() always returns 0x01. Why is that? This, in contrast, works perfectly fine:

Code:
char i=45;
while(i!= EOF)
{
i=getchar();

Shouldnt an assignment always return the assigned value?

View 2 Replies View Related

C :: Making Calculator Program Using Only Getchar And Putchar

Mar 21, 2014

I have to make a calculator program that has 5 different functions (this is obviously the easy part, once all the input is assigned to their respective variables), which are addition, subtraction, mod, multiplication and division (negatives are not allowed, and must be error-checked; spaces must be ignored). I need to take the equation from the user, echo it back and then solve the problem.

Code:

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

[Code].....

View 4 Replies View Related

C :: How To Put Value From Scanf Into Array

May 2, 2013

Code:

#include<stdio.h>
#include<windows.h>
int nr, i, array_new[10],n=0;
main(){
printf ("Insert number: ");
scanf ("%d", &nr);

[Code]...

I need put value from scanf into array. How make this: array_new[nr]?

View 8 Replies View Related

C :: Using Scanf In If / Else Statement

Aug 7, 2013

When I do this:

Code:

#include <stdio.h>
#include <stdlib.h>
int main () {
int guess ;
int number = 5 ;

[Code] .....

But when I do this:

Code:
#include <stdio.h>
#include <stdlib.h>
int main () {
int guess ;
int number = 5 ;

[Code] ....

Why does the compiler say that there is an else without a previous if ??? ... Does this mean that I cannot use "scanf" in an if/else statement?

View 2 Replies View Related

C :: How To Scanf To Integer Or Char

Feb 6, 2014

I have to make a program for school to evalute poker hand.I am not asking about the logic of the program,I am asking because I have to take the input from the console,five lines which can be the cards 2,3,4..10 and the problem is with J,Q,K,A ,because I dont know how to tell scanf to expect integer or char ? If I type J,Q,K,A the program crash ,

View 1 Replies View Related

C :: Getting Char As Input By Scanf

Mar 31, 2014

In GCC v4.6, I am encountering a strange problem. If I try to get a character input and a int input like this:

Code:

int main()
{
int a;
char b;
scanf("%d",&a);
scanf("%c",&b);
}

the compiled program asks for input only one time. On the other hand, if I do the char scanf earlier like this:

Code:

int main()
{
int a;
char b;
scanf("%c",&b);
scanf("%d",&a);
}

it asks for input twice.

View 6 Replies View Related

C :: How To Write Scanf With Right Parameters

Jun 30, 2013

How can i upgrade my program I want to input a octal and binary number and convert them in base 2, 8, 10, 16..

how can I write the scanf with the right parameter in it??

Code:
scanf ("%x",&i); Code: #include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main() {
int i;
char buffer [33];

[Code] .....

View 5 Replies View Related

C :: Scanf / Fgets Without Echo

Aug 31, 2014

I am trying to write a terminal-like chat application in Linux. I would like to use a FIFO queue to print out the messages in terminal. The queue would be populated from 2 sources- stdin and messages sent from the other user over TCP. I have meet an obstacle that I cannot handle...

Lets say I would like to take user input using fgets and put it into a buffer. Then queue it if the buffer is not empty or print if it is. The problem is that when I use fgets or scanf, my input is instantly printed to the terminal..If i do:

Code:

fgets(message, 100, stdin); printf

("%s", message The string under message is printed twice :|. Is there a way to prevent this?

View 5 Replies View Related

C :: Scanf With Stopping Condition

Mar 22, 2013

I wanted to input some numbers with scanf function, i can enter some numbers and if I input -1 to the scanf, the input must end. And the scanf function has limited input, the max that I can input is 40 numbers.example if enter 1 2 4 6 5 4 -1 the scanf function will ended and the result will be appear.I wanted to know how the scanf function is like that would be best for this problem, Code: scanf("%d", &n); the result if I input those number will be like

|
||
||||
||||||
|||||
||||

View 3 Replies View Related

C :: How To Scanf Inputs Into Array

Nov 23, 2014

I don't know how many numbers will be the input only that its going to be up to 10000. EOF should be active. I tried it like this:

Code:

int i = 0;
int del[10000]
while (scanf("%d",del[i])!=EOF)
{
i++;
}

But it seems the value of i doesn't increment, could you provide some tips on how to scanf the inputs into an array if I don't know how many numbers will I have?

View 7 Replies View Related

C :: Scanf Is Skipped In Run Window

Jul 11, 2014

when i run it , it skips scanf and not allowing user to enter input.tried adding getchar(), fflush but nothing worked for me.
//file name : sort_array_of_structure.c

Code:

/*Write a program to accept records of the different states using array of structures. The structure should contain char state, population, literacy rate, and income. Display the state whose literacy rate is highest and whose income is highest.*/

#include <stdio.h>
#define M 50
struct state
{
char name[50];
long int population;
float literacyRate;
float income;
}

[code]....

View 3 Replies View Related

C++ :: How To Flush Scanf Buffer In C

Oct 10, 2014

I am writing a program and am asking the user to enter a number between 1-12.

If they do not, they will be promoted over and over again to enter a number between 1-12.

However, if the user enters a letter or anything else scanf seems to stop working and then my loop goes out of control.

View 2 Replies View Related

C++ :: Input Using Printf / Scanf

Oct 28, 2014

How to store the value in this case.. The topic is called "limited summation".. The following is the guideline for this problem:

Write a program in a folder called "sum" with a source file of main.cpp that does the following:

•prompt a user to enter a maximum number of entries, make the prompt "max # entries"

•prompt a user to enter a threshold sum, make the prompt "threshold"

•using error checking logic, let a user enter base-10 signed numbers until at least one of the following conditions is true:

or the maximum number of entries is reached
or the sum of entered numbers has reached (>=) the threshold

•print the sum of all the entries, just the number and a linefeed at the end of the line

Error checking means entries that are not numbers are detected and ignored. You are to use printf and scanf in this assignment (no cin or cout).

View 3 Replies View Related

C :: Scanf Input By The User Into Function

Apr 19, 2013

I tried to scanf the input by the user into the function but it does not read my input. Read on one of the thread, it said that to scanf a input into a double, need to use %1f instead to %d (which normally used. I tried changing it to %1f and it still did not work.

Code:
#include <stdio.h>
#include <math.h>
/* function main begins program execution */
int main( void )
{
double amount; /* amount on deposit */

[Code] ....

View 2 Replies View Related

C :: Trying To Scanf A Char And Code Just Jump Over It

Jan 25, 2014

I'm doing a code to calculate the final grade of students. This is a work for college, and I need to keep this structure.

My problem is that last scanf, it is ignored when I compile the code. It "works" if I try to scan a string, float or int.

Code:

#include <stdio.h>
#include <stdlib.h>
void nfinal(float NOTA1,float NOTA2,float NOTA3,char MEDIA){
int NOTA;
if(MEDIA=='A'){
NOTA=(NOTA1+NOTA2+NOTA3)/3;

[Code]....

View 3 Replies View Related

C :: How To Scanf Global Variable Outside The Main

Sep 7, 2013

The problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in it ,but the "nbColours" unknown and I have to get it get it from a file .

Code:

int nbEdges,nbVetices, nbColours ;
typedef struct st_graphVertex {
int index;
int colour;
int val ;
int vertexDegree[nbColours]; // it won't work because nbColours unknown
// here and I want get it from file in the main
struct st_graphVertex *next;
t_edgeList *out;
}t_grapheVertex;

View 3 Replies View Related

C :: Scanning 2 Strings In Same Scanf Call

Nov 20, 2013

Somehow only str2 is successfully scanned and str1 is not printed

Code:

printf("
Please enter two times in this way xx.xx xx.xx now ");
scanf("%s%s", str1, str2);
printf("
%s - %s:
", str1, str2)); result : - str2:

View 3 Replies View Related

C :: How To Exit Out Of Scanf After Certain Amount Of Time

Oct 25, 2014

I'm trying to make a very simple reaction game where a random number flickers on the screen for a fraction of a second and the user has to then enter the number before another comes on the screen after a random amount of time. However I dont know how i would make it so that the user cannot enter anything after a certain amount of time has passed, below is my code?

Also FYI, clock_start is at 5100 because by the time the program actually gets to scanning in the first number the time is at an absolute minimum of 5050 milliseconds however obviously this is an impossible number to reach due to processing, my machine clocks in at 5080.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int main(void){

[Code]...

View 6 Replies View Related

C :: Scanf Is Returning Different Vales On Different Machines?

Mar 30, 2014

I have created a program using a switch statement that uses scanf() to accept a users input. The input variable is defined as an integer and scanf() is also looking for an integer.

One computer I run the program and it will return a value of 0 for all non-numerical inputs such as 'z', and the output thinks the value 0 was input into scanf(). On another computer when I input 'z', the compiler stores this as 198864884, and my program executes correctly giving an error.

View 9 Replies View Related

C :: How To Signal End Of Standard Input Using Scanf

Oct 30, 2013

so i'm using scanf() this way

Code:

int i;
while (scanf("%i", &i))
printf("%i ", i);
printf("
done
");

i tried several combination of Ctrl+D and "Enter", it's not terminating, Ctrl+C just "cancels" the whole thing without printing "done", i'm running this on linux installed on a PC

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved