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


ADVERTISEMENT

C :: Printf And Scanf - Output String Data At The End Of The Line

Dec 2, 2013

I'm trying to get this programme to work but I can't get it to output the string data at the end of the line.I have copied and pasted the line in question below but it may be a prob with the prog further down.

It reads character input ok but doesn't put one string into another or recognize when a string is quoted in a printf.

Code:
printf("%s what is your second name?
", surname, name2, name);
#include <stdio.h>
int main ()
{
char name[20];

[Code] ....

View 8 Replies View Related

C :: Why Cannot Pass The Address Of String During Printf Or Scanf Functions

Jan 24, 2013

Why do we not pass the address of the string during printf or scanf functions like we do for Integer or float variable types?

View 2 Replies View Related

C :: Multiple Scanf Calls Skip Printf Lying Between Them

Mar 31, 2014

Code:

scanf("%d", &a);
printf("A");
scanf("%d", &b);

prints "A" after calling scanf two times instead of between the calls (first scan, then print, then scan). I'm using GCC v4.6

View 7 Replies View Related

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 :: 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 :: 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 :: 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

C :: Using Scanf To Read A Character From Standard Input

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

C/C++ :: If Statement Comparing Array To Scanf Input

Oct 25, 2014

The issue that I'm having is that when I run this part of the code it seems to enter the if statement and set it to 0 when comparing the array to the input.

What I want it to do is to search the array for the grade, set it to zero, and then exit the loop in case there are any repeat grades. It always sets the first number to 0 and then exits.

//Below is the prototyping for the array

int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};

//There is some code between here, but it is probably not relevant so I didn't post it.

printf("Enter the grade that you want to remove. Press Enter after each input.");
printf("Enter 0 to exit: ");

[Code] ....

I didn't mean to leave the "prototyping" part in the code. I was initially going to put the function prototypes in there, but decided it wasn't necessary for the question.

View 6 Replies View Related

C :: How To Printf Floats

Jan 30, 2013

I am having a hard time with some of my homework, specifically regarding how to printf floats. I can't seem to print the number i want out using float, it just becomes a jumbled mess.

Code:
#include <stdio.h>
#define TICKER "LRCX"
#define PURCHASE_DATE "01/02/13"
#define SELL_DATE "01/30/13"
#define INVESTMENT_AMOUNT "10,000.00"

[Code] .....

Thats the code I currently have, I've probably tried everything to get the number to come out, but I just cant seem to figure it out. It should look like this, but with different numbers and stock:

Stock: MCD Buy Date: 01/02/13 Sell Date: 01/29/13 Buy Share Price: $89.40 Sell Share Price: $91.50 Shares Purchased: 111.86

Amount of Investment: $10,000.00 Value of Shares Sold: $10,234.90 Amount of Gain/Loss: $234.90 Percent Gain/Loss: 2.35%

However, this is how mine turns out:

Code::Blocks
Enter share purchase price for LRCX=>23
Enter the selling price for LRCX=>23

Stock: LRCX
Buy Date: 01/02/13
Sell Date: 01/30/13
Buy Share Price: -1.#R
Sell Share Price: -1.#R
Shares Purchased: -1.#R

Amount of Investment: 10,000.00
Value of Shares Sold:-1.#R
Amount of Gain/Loss:-1.#R
Percent Gain/Loss:-1.#R%

Process returned 0 (0x0) execution time : 2.864 s
Press any key to continue.

View 3 Replies View Related

C :: How To Printf A Struct

Apr 22, 2014

Code:

#include <stdio.h>
struct database {
int id_number;
int age;
float salary;

[Code] ....

When I compile, I get an error:
test.c|18|error: incompatible type for argument 1 of 'printf'|
note: expected 'const char *' but argument is of type 'float'|

I thought employee.salary is a float but the compiler expected 'const char'. How do I make this work?

View 4 Replies View Related

C++ :: From Printf To String

May 6, 2013

I have the following line of the code. Now I want to save the content to a string. Is there a quick way for me to do the conversion using the same arguments/codes of printf?

printf ("Some different radixes: %d %x %o %#x %#o
", 100, 100, 100, 100, 100);

View 3 Replies View Related

C/C++ :: How To Skip The Value In Printf

Sep 5, 2014

#include<stdio.h>
void main()
{
int i=6;
printf("%d %*d
",i,i+9);
}

what is the out put?

View 2 Replies View Related

C++ :: Printf In Loop

Jul 17, 2013

I am having trouble with the printf in this function:

Code:
void print_orig_array(char string_array[MAX_PEOPLE][NAME_SIZE], int ages[MAX_PEOPLE], int length) {
int counter;
printf("Original list");
printf("

[URL] ....

Here is the output:

Am I missing something with the format specifier? How do I fix the 84 that gets pushed out?

View 2 Replies View Related

C :: Printf In A While Loop Is Printing Twice Instead Of Once

Sep 21, 2013

I have a minor issue in my program:

Code:
char Answer;
printf("
To search for a specifc word, type (Y), to use a dictionary file, type (N):

[Code] .....

When I run the program, it gives:

To search for a specific word, type (Y), to use a dictionary file, type (N):

Sorry, the given input is invalid, please try again:

I can then input the number. Typing n,N,y or Y goes to the next part of the program without any problems, but if I type something else, I get:

Sorry, the given input is invalid, please try again:

Sorry, the given input is invalid, please try again:

I've fiddled about with the code for a while now, but nothing I do seems to work. What is causing it to be printed twice, or why the first getchar() is ignored.

View 2 Replies View Related

C :: Printf Int That Was Defined In Another Function?

Feb 26, 2013

I'm making my way through most of this assignment that I have, but now it seems like I've run into a bit of a roadblock. The issue that I'm having is not being able to printf a series of ints that I thought I had previously defined in another function. I don't want to clog up this post with the entire code, so I'll just post one function that defined an int to give an example. I will upload the whole thing upon request however.

Code:

#include <stdio.h>
#include <stdlib.h>
//Prototypes
int AGrade1(int* grade1);
int AGrade2(int* grade2);
int AGrade3(int* grade3);

[Code] .....

I've tried many many things, but I just cant figure it out. This is what it's supposed to look like.

Assignment Grades:
18 12 17 15 20 13
20 18

View 9 Replies View Related

C :: Float Showing Nothing In Printf

Oct 6, 2014

I just checking but confused with float. in that code same size int, and same type double are working but float showing nothing in printf..why?? i'm using GCC compiler int 32bit win7 os

Code:
#‎include <stdio.h>
int main() {
char arr[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
printf("Size of char=%c
", ((char *) (&arr[0]))[1]);

[Code] ....

View 14 Replies View Related

C :: How To Center Text Using Printf

Jul 23, 2014

The overall width of the line is 20 characters. How can I center a string using print so that

title becomes
space-title-space

i have something like this so far Code: fprintf(myfile, "%20s ",mystring);

View 2 Replies View Related

C/C++ :: Why Does Printf Not Print After A While Or For Loop

Jul 1, 2012

I would like the following code to print: "Why doesn't this print?" and "I would like to print the sum of nc: 5". What am I doing wrong.

#include <stdio.h>  
//Use to test ideas and formats//  
main() {
    int c, nc;      
    nc = 0;  

[code]....

My result as compiled by gcc -o testing testing.c

This prints.

test
t1,e2,s3,t4,
5,

I have not figured out how to sum and print as the above code indicates, which complicates my ability to do many of the exercises in "The C Programming Language". I am using a MacBook gcc compiler and X code as well. I cannot get the last two printf functions to work. I did the temperature example with "while (fahr <= upper)" and the printf printed.

View 7 Replies View Related

C :: Printf Doesn't Print On Screen

May 23, 2013

This code runs. However, when I run it, the text from printf doesn't appear until after I type in the two numbers.

I use Wascana, will it run correctly on other compilers?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
fflush(stdout);

[Code].....

And this is how it turns out on the screen:

Code:

6
3

What size is the die:

how many dice to roll:

Numbers are 3d6
Rolling die no.1...
RolledDie: 4 DieTotal: 4
Rolling die no.2...
RolledDie: 5 DieTotal: 9
Rolling die no.3...
RolledDie: 5 DieTotal: 14

The total is: 14

View 4 Replies View Related

C :: Global Variable - Only Printf Works

Apr 20, 2013

Okay so I am programming an 8051 variant to interact with an SD card. In a separate initialization routine in SD_CARD.c I pull out the vital information of the card into global variables. I then call Menu() which is in another source file but includes a header file with all of the variables declared as extern. Now here is the weird, part this is from my Menu.c

printf("%u" , VOLUME_1_SECTOR);
if(VOLUME_1_SECTOR==16384)
printf("Correct");
else
printf("Incorrect");

Now the output of the first printf is 16384 but the conditional evaluates to false. If I put this code in SD_CARD.c (Where VOLUME_1_SECTOR is defined) the conditional evaluates to true. I am confused why the printf works correctly in Menu.c but not the conditional.

View 2 Replies View Related

C :: How To Copy Result Of Printf Into Array

Oct 12, 2013

I am trying to compare 2 strings of characters The users input containing 5 chars is compared to a table If the input is already be existent in the table the index of those chars in the table is printed Quest: how to copy the result of a printf() into an array ? The last printf() gives a sequence of numbers and I am trying to save that sequence to another array for further operation ! I have not been able to do that so far even with tmp[]=i ;

Code:
#include <stdio.h>
#include <string.h>
#define N 30
#define n 100
int main (void)
[code]....

View 2 Replies View Related

C :: Code Won't Execute Last Call Of Printf

Nov 10, 2014

I'm trying to create a program that will show what the 12th digit of a UPC code would be. However, once the user enters the first 11 digits the program doesn't execute the last call of printf. The program compiles with no issues.

Code:

#include<stdio.h>
int main() {
int o1, e2, o3, e4, o5, e6, o7, e8, o9, e10, o11, oddsum, evensum, twelve;
printf

[Code] .....

View 9 Replies View Related

C :: Cast To Character Pointer For Printf

Apr 18, 2014

I cannot get the following to compile. The problem is the printf on the last line. I understand that printf requires a char (or pointer to char). I understand that I can convert between datatypes by putting the target data type in parenthises in front of the variable. But how do I cast the integer into a character and then get it's pointer to pass into printf?

Following is my code. I compile with gcc temp.c -o temp.

Note that I have tried many attempts at that last line and this is just the one that I really, really think should work (or is at least the closest to the correct answer).

This code shown below, using printf("%s", &(char)nextChar); returns

temp.c:26: error: lvalue required as unary '&' operand

If I try to use printf("%s", *(char)nextChar); I get the error

temp.c:26: error: invalid type argument of 'unary *' (have 'int')

This line printf("%s", (char)nextChar); returns the obvious

format '%s' expects type 'char *', but argument 2 has type 'int'

Code:

#include <stdio.h>
int main() {
printf("hello, world
");
#if defined(SUNDIALS_EXTENDED_PRECISION)

[Code] ....

View 5 Replies View Related

C :: Printf Ints That Are Modified In Another Function?

Feb 13, 2013

I have gotten it to record the date and I can printf it either on the same function, or in the main(). However, one of the requirements I must adhere to is to printf the statement in a brand new function, but when I do that, it just doesn't work. Heres what I mean:

Code:

#include <stdio.h>
#define TICKER "LRCX"
#define INVESTMENT_AMOUNT "10,000.00"
//Prototypes
int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2);
float getprice(float* BPrice, float* SPrice);
void printdate(int month1, int day1, int year1);
}

[code]....

View 3 Replies View Related







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