C/C++ :: Reading Number In CPP Program With Scanf
May 17, 2014
I'm using scanf() function to read numbers in my cpp program and, for example, if i want to read a string that is a complete name i could use
scanf("%[^
]", x);
or if i want to read only vogals i could use
scanf("%[aeiou]", x);
Is it possible to read only positive numbers or only negatives numbers using scanf() and how to do it?
View 9 Replies
ADVERTISEMENT
Feb 5, 2015
I trying to write a program able to read a number up to 3 digits and display it in words. What to do first.
View 1 Replies
View Related
Nov 3, 2013
I need to write a program that will allow the user to enter a number "n" and the program tell you that the nth prime number is .....
EXAMPLE
user enters 55
printf("The 55th prime number is %i", variable");
View 1 Replies
View Related
Feb 27, 2015
lets say this is our textfile
-----------------------------------
45(here is space)string and other shit is here
454(here is space)string and other shit is here
4121(here is space)string and other shit is here
77(here is space)string and other shit is here
45545(here is space)string and other shit is here
1122(here is space)string and other shit is here
-----------------------------------
how do i get exactly that number in every line start? and compare but i jus tneed to get them to variable so ?
View 3 Replies
View Related
Apr 14, 2014
Windows 7, 64 bits, Visual Studio 10.
I have a problem to read a large number of binary files, process them and store them under a new name. The program and routines go very well for 505 files. After reading 506 files, the program now refuses to read the next file. I have 16 Gb of memory and tried to close all other programs and restart the PC. it always stops after 506 files (512 files would be more understanding in a way...).
Here is my code. I have tried many things without success. This is only part of the loop that stops. The if test if (myfile.is_open() returns false by some reason. I can start the process again starting with the file that does not open and then it stops again after 506 files.
char * tfiBlock;
ifstream myfile (OrigFilename, ios::in|ios::binary|ios::ate);
if (myfile.is_open()) {
int lengde = myfile.tellg();
tfiBlock = new char [lengde];
//static char memblock [size];
[Code] .....
Clean up procedure:
delete[] tfiBlock;
Are there any limits to how many files that can be opened, or is it maybe someting to be set in the compiler?
View 5 Replies
View Related
Mar 1, 2015
I have a file with such a format:
string string 8 7 2
string string 7 12 19 -4
string string 1
As you can see I don't know how many numbers there are at the end. I have this code now:
while(!in.eof()) {
kr = 0;
getline(in, pavard, ','); getline(in, vard, ','); getline(in, gr, ',');
while(in.peek()) {
in >> k;
kr += k;
}
in.ignore();
}
in.close();
I tried various combinations with peek() function, but nothing worked.
View 2 Replies
View Related
Jan 16, 2013
Consider the following piece of Code:
int ReadNumbers() {
int num;
vector<int> x;
cout << "Enter Numbers" << '
[Code] ....
The while loop is expected to terminate when the user provides an Invalid Input. But this while loop behaves unexpectedly when the user provides a 'Newline' input (by pressing Enter) and becomes an infinite loop. How can I prevent this from happening? Also I've heard that expecting invalid inputs isn't good code design. Is this true? If yes, then how can I solve my question without expecting Invalid Inputs?
View 10 Replies
View Related
Nov 29, 2013
How can I read a file that contains numbers only, but read it by three digits at a time? I have a long string of numbers and every three digits corresponds to a particular number in itself. i.e. a string of 064045154 would need to be read as '064' '045' and '154'. I need to then subtract one from each of these numbers and the new values I need to convert into their ASCII characters and place these in a new file. This is what I have (focusing on the 'Decrypt' function) but all it does is in the new file place a string of the same character repeated over and over a total number of times equal to the number of integers in the numbers file.
Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "limits.h"
int Encrypt(char * FILENAME)
[Code]..
View 1 Replies
View Related
Aug 20, 2014
In the c pgm to find number of digits , if I am giving 001 as the input number ,why I am not getting the no. of digits as 3?
View 2 Replies
View Related
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
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
Sep 10, 2014
I just took up c programming and like it so far. Now I got a problem reading in a character in a program. The program looks like this:
insert
Code:
#include <stdio.h>
main()
{
char c1, c2;
printf("Letter 1: ");
c1 = getchar();
[Code]....
View 8 Replies
View Related
Feb 13, 2015
how to read file twice in a single program?
View 4 Replies
View Related
Jan 24, 2014
I have a program that makes a .txt file and I am trying to read it and it won't.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fershizzle;
[Code] .....
View 13 Replies
View Related
Mar 19, 2013
I'm working with parallel arrays, one handles the titles and the other handles the ratings. Now everything works fine if the title doesn't start with a number, but if I decide to use a number infront of the title, when it reads it from the .txt file it thinks it is part of the rating.
Here is what I mean:
1234567891011121314151617181920212223242526272829303132333435363738394041424344
LOADING DATA FROM FILE...
MENU
1. Add Movie
2. Print All
3. Exit
------------------------------------------------------
Enter 1-3 : 2
PRINT ALL
------------------------------------------------------
Hitch 98% FRESH
Happy Gilmore 99% FRESH
Transformers 2 100% FRESH
SpongeBob 10% ROTTEN
MENU
[code]....
As you can see, the '2' from 2 Fast 2 Furious went to the rating in SpongeBob.
Here is what it looks like inside the .txt file:
Hitch| 98Happy Gilmore| 99Transformers 2| 100SpongeBob| 102 Fast 2 Furious| 100
Here is my code:
void SaveToFile(const string titles[], const int ratings[], int count)
{
string filename;
ofstream outfile;
filename = "tomatoes.txt";
outfile.open(filename);
PrintToText(outfile, titles, ratings, count);
WriteLine(cout, '=', 54);
[code]....
View 3 Replies
View Related
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
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
View Related
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
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
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
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
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
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
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
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
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