C/C++ :: Program That Read A Number From Keyboard And Print Separated Digits To Screen
Feb 18, 2015
Basically this is what i need to do. Write a program that reads a number from the keyboard, separates it into its individual digits and prints the digits to screen, each on its own line followed by the same number of stars as itself.
For example, if the number is 2339 the program should print
9 *********
3 ***
3 ***
2 **
So far i have managed to separate the number and have them on different lines, but how to implement the stars onto each line with the number!
My code so far:
int main() {
int n;
printf("number? ");
scanf("%d", &n);
while (n > 0) {
printf("
%d
[Code]...
View 4 Replies
ADVERTISEMENT
Oct 16, 2012
I have been trying to read a comma separated .txt file into an array and print it to console in C++. The txt file consists of two columns of double type data. For some reason the program runs, but gives blank output in the console. I want to know if I am doing something wrong. So far this is what I have:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
int i=0;
double x[10];
double y[10];
string line;
[Code] .....
View 3 Replies
View Related
Mar 24, 2014
write a program in c to read a sequence of N integers and print the number that appears the maximum number of times in the sequence.
View 1 Replies
View Related
Feb 26, 2013
is there anyway to read an entire file and print in on screen? file consists of strings and integers!
View 7 Replies
View Related
Dec 25, 2014
Print the inverse number. Ex: 2178*4=8712
Write down a program which can satisfy the prerequisite and print out the screen.
The answer is:
Code:
void inverse2 ()
{
int i,j;
for (i=1000;i<=9999;i++)
for (j=1;j<=9;j++)
if (i*j==inverse (i))
printf("%5d%2d",i,j);
}
I can't understand the double for loop.
for (i=1000;i<=9999;i++)
for (j=1;j<=9;j++)
And The meaning for
i*j==inverse (i)
View 2 Replies
View Related
Mar 8, 2013
My objective is to check to see if a number is odd or even and print it to the screen just to make sure its working correctly.
The data file contains:
138
402 2 485
9
43
98 805
58
421
948 7 97
48
67
35
42
948
579 39 12
700 20 500 210
98
I can get the numbers to print fine using:
fstream infile1(argv[1]);
if(!infile1.is_open())
cout << "Could not open file";
else {
char listNum;
while(infile1.get(listNum))
cout << listNum;
}
However, when I check for odd or even numbers it will check each and every number.
printed like this (partial list):
1 is odd 3 is odd 8 is even
9 is odd
But it should print:
138 is even
9 is odd
I tried using getline, but it keeps giving me the errors:
invalid conversion from 'void*' to 'char**'
invalid conversion from 'char' to 'size_t*'
too few arguments to function 'ssize_t getline(char**, size_t*, FILE*)'
Here is the getline code, what am I doing wrong? I have tried switching things around, adding things. Just nothing works.
ifstream infile1(argv[1]);
if(!infile1.is_open())
cout << "Could not open file";
else {
char listNum;
getline(infile1, listNum);
cout << listNum;
}
View 2 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
Oct 11, 2014
My problem needs to prompt the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. An example would be entering 8030 and it spits out 8 0 3 0 as well as 8+0+3+0=11 and it needs to work with negative numbers.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int base;
[Code] ....
Now I don't know if any of this is right a hint my professor gave us is that to get the fourth digit you would do base mod 10 and to get the first digit you do base divided 1000...
Code:
{
int power;
int counter=0;
int value=1;
cout << "Enter the power of 10 you want: ";
[Code] ....
View 2 Replies
View Related
May 29, 2013
I'm trying to write a program that prints all of the prime numbers to the screen. I'm getting angry because it only prints EVERY number. I think it has something to do with the bool change not being able to leave the for-loop but how to fix it.
Code:
#import <stdio.h>
#include <stdbool.h>
int main(void){
printf("
The Primes Are: 1");
[Code] ....
View 3 Replies
View Related
Oct 10, 2014
Why this program won't correctly print the proper totals on screen? See my code and the included screenshot.
Code:
#include <stdio.h>
int main(void) {
int total, first, second;
printf("Enter 2 numbers:
[Code] .....
View 4 Replies
View Related
Jan 16, 2014
I've been working on a program that displays the number of digits in each line of a file, but I feel stuck. Take for example a file that contains these characters:
6347aaa9
54j
811111
6a
709
And I'm trying to display a result like this
1 //that's the number of the line 5 //the number of digits
2 2
3 6
4 1
5 3
Here's what I've written so far:
Code:
#include<stdio.h>
int main() {
char a=0;
int number_of_digits=0, linescount=0, num, number_of_digits_per_line=0;
FILE *inputFile;
if(!(inputFile=fopen("C:TestTest.txt","r")))
[Code]..
I also thought of using fgets and strlen but I am not very good with them and couldn't get the program to work correctly. It did work but it displayed all characters, letters included, not only digits.
View 2 Replies
View Related
Dec 3, 2013
How to move a character (an arrow or any other character) on screen using keyboard? I'm actually a beginner and have only wrote simple (starter's) programs in C++. It would be great without using any external library which is not included in C++ by default (in other words, using plain C++).
View 8 Replies
View Related
Mar 30, 2014
I'm trying to write a C++ program that will allow a user to input a number from the keyboard. Then using a loop, that will perform 10 times, multiply the entered number by the loop counter. Print out the loop counter, the entered number and the product of the loop counter and the entered number. A one-time heading should be displayed before information is printed.
This kinda of what I have so far:
#include <iosteam>
using namespace std;
int main () {
Start
Declare: numScores, sum, score, avg, SENTINEL = 200
numScores = 0
[Code] ....
All the programs I have tried to make are not working?
View 4 Replies
View Related
Dec 6, 2013
By using visual studio 2010, I have problem calling up on screen keyboard using line below for Windows 7 64 bit.
WinExec("OSK.EXE", SW_SHOW);
However, it is working fine on windows XP 32 bit. How to call up on screen keyboard for 64 bit windows?
Code:
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL) {
// initialize MFC and print and error on failure
[Code] ...
View 2 Replies
View Related
Dec 8, 2013
Is possible read text form keyboard using read() function? or which is best way in ansi creplace input command form basic language. for numbers and text...
View 3 Replies
View Related
Nov 22, 2014
I'm trying to enter an 'x' and 'y' coordinate on only one line separated by a comma. But I keep getting a syntax error. Here are the lines of code I'm using. This has to be simple. What am I doing wrong with this code?
Code:
cout<< "Please enter the x and the y coordinates of the first point,"<<endl;
cout<< "use a comma to separate them. " <<endl<<endl;
cin>> "You entered: " >>x1>>",">> y1 >>"for the first point" >>endl;
View 7 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
Apr 21, 2013
Write a for loop statement to print the numbers from 1 to 10 in reverse order separated by star :
10*9*8*7*6*5*4*3*2*1
i try to do it but it show me like this :
10*9*8*7*6*5*4*3*2*1*
how do i write to show me like the first one ?
View 10 Replies
View Related
Apr 6, 2014
My C programming class wants us to write a program to read integers into an array, sort the numbers and print out the occurrence of each number. I've tried everything that I can think of but the core dump is still occurring.
void countValues ( FILE *inf, int list[], int size ); /* Function prototypes. */
void printFrequencies( const int list[], int size );
int main (void) {
int status = EXIT_SUCCESS; /* defaulting status to success. */
FILE *inf = fopen( "numbers.txt", "r" ); /* input data file */
[Code] .....
View 6 Replies
View Related
Sep 16, 2013
I have prepared a file through the use of following code
Code:
fprintf(file2, "%i %i %i %i %i %i
",
msg_id,
msg_size,
msg_period,
msg_deadline,
msg_producer,
msg_comsumer
);
As one can see, this file has tab separated integer entries. The file is written correctly, let us call this file "msg.txt".
I face the problem when I read this file, using fgets as follows:
Code:
char singleMessage[100];
while( fgets(singleMessage, sizeof(singleMessage), file ) )
{
puts(singleMessage);
sscanf(singleMessage, "%i %i %i %i %i %i
",
&first, &second, &third, &fourth, &fifth, &sixth);
fprintf(stderr, "first: %d, second: %d, third: %d, fourth: %d, fifth: %d, sixth: %d
",
first, second, third, fourth, fifth, sixth);
}
but fgets only retrieves until the first, i.e, if the first line in the file reads:
788004425
fgets returns only 78.
Does it have to do with how the file was written in the first place.
View 1 Replies
View Related
Nov 9, 2013
I have a function reads from a file like this
file
foo;bar
foo;bar
foo;bar
function
void EntryList::loadfile(const char filefoo[]){
ifstreamin;
[Code] ....
I am in the middle of rewriting this program for at least the 4 time. and I have modified the file how I (humanly) think I should to this. I have had issues in the past, doing it this way. (still working on the other parts of the program so I cannot be too specific right now, but I know my results were unexpected ) So my question is does the function that I modified look correct for what I am trying to do? Am I off by one? I guess I am struggling with understanding how the original function is working. (step by step systematically.) hence my confusion about my modified function.
View 2 Replies
View Related
Jul 30, 2014
I just wrote a code that reads in a text file called "policies.txt" which looks like this:
Pol1 M N 20 100000 1 .04 99
Pol2 F S 30 100000 1 .05 99
Pol3 M S 72 750000 1 .03 99
Pol4 F N 45 1000000 1 .05 99
This works perfectly fine, but what if I want each element of the table to be separated by commas. For example:
Pol1,M,N,20,100000,1,.04,99
Pol2,F,S,30,100000,1,.05,99
Pol3,M,S,72,750000,1,.03,99
Pol4,F,N,45,1000000,1,.05,99
or better yet, what if I want it to not matter whether the columns are separated by commas or spaces? is there any way to do this? If there is no way to read in both comma-separated and space-separated elements simultaneously then I would prefer just comma, rather than the space separated which my code is able to read now. What modifications would I have to make to my code to make this work? This is my code to reference.
double ratesmn[86] = {
#include "MaleNonSmoker.txt"
- 1
};
double ratesms[86] = {
#include "MaleSmoker.txt"
[Code] ....
View 4 Replies
View Related
Dec 14, 2014
That is sequence "aasdf123456785fg87" will be transformed into "aasdf12345678fg". I need to do this task in two variants: at once using getchar and putchar and then with strings. The object is done, but I think it could be done more easier. My codes:
(1)
char flag = 0;
while ((c = getchar()) != '.')
{
if (isdigit(c))
[Code]....
View 5 Replies
View Related
Nov 18, 2013
I am trying to Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. How do i go about it. What i dont know is how to come up with smallest and largest number .
View 1 Replies
View Related
Mar 27, 2013
char name[10];To read a string from keyboard.
what should i use :
this : cin>>name[10];
or
this : cin>>*name;
View 3 Replies
View Related
Mar 14, 2013
Objective of this program is to read binary data from a file and print it.
Code:
#include <stdio.h>
void readFile(void);
int main(){
readFile();
return 0;
[Code] .....
But When I Run This Code First It Print "Error." Then Rest Of The File.Say In My File I Have "I AM HUMAN", It Prints "Error. HUMAN"!!
I Cant Figure Out Whats Wrong In The readFile() Function.It seems right to me.
View 4 Replies
View Related