C++ :: How To Stop Input After Certain Characters
Oct 23, 2013
I'm trying to get the user to input their 5 digit zip code but my problem is how can I stop the program after the five characters have been inputted. Also how can I only get numbers to be inputted cause so far letters can be inputted as well.
View 2 Replies
ADVERTISEMENT
Jan 18, 2014
It's a tic-tac-toe program. I haven't finished doing everything I need to for the project, I'm just trying to get certain parts working as I go.
Anyway, my problem: The program will print the board and ask the first player which square they want to mark. Then it will print the updated board, print the question for the second player to input their square choice but not actually stop to let the user enter anything. Instead it prints the board again and then prints the question for player one again -- this time stopping to let them type in their choice.
Here's my code:
#include <stdio.h>
// Function prototypes
void printBoard(char board[3][3]);
int main() {
int quit = 0;
// Loop so game continues until player quits
[Code] .....
And it looks like this when I run it, as an example:
-------------
| 1 | 2 | 3 |
-------------
| 4 | 5 | 6 |
-------------
| 7 | 8 | 9 |
-------------
Player 'C' enter a square: 5
-------------
| 1 | 2 | 3 |
-------------
| 4 | C | 6 |
-------------
| 7 | 8 | 9 |
-------------
Player 'U' enter a square:
-------------
| 1 | 2 | 3 |
-------------
| 4 | C | 6 |
-------------
| 7 | 8 | 9 |
-------------
Player 'C' enter a square:
View 11 Replies
View Related
Sep 27, 2013
how to stop outputting data based on a user input. The text file is as follows:
1. a
2. b
3. c
and the code I'm using is as follows:
int main (){
string line;
int search;
cout << "Enter a number from 1-3" << endl;
cin >> search;
search++;
ifstream myfile ("example.txt");
[Code]...
What I want to do is to just output number 1 (the whole line) if the user enters number 1. However, I get an error on the second condition w/c is the "&& line!= search"
View 1 Replies
View Related
Mar 27, 2014
I am trying to make sure no floating point numbers can be input at any time in my roulette program. My code is too long to put in one post so I shall piece it out
#include <iostream>
#include <iomanip>
#include <random>
#include <cstdlib>
#include <ctime>
using namespace std;
// structure of arrays to pass to functions
[Code] .....
View 12 Replies
View Related
Jul 25, 2014
int a[20],x;
for(x=0; x<20; x++)
{
[Code]....
This should be the output
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: 1
Input: -1
Input: -1
Input: -1
Input: 1
Input: -1
Input: 1
Input: -1
Stop!
View 5 Replies
View Related
Nov 25, 2013
I want the numbers to be put into the array until it hits a new line. Maximum number of numbers is 1000. What can I replace that while loop with to stop it from reading further into the input file?
Code:
while (input != '
'){
for(i=0; i<1000; i++)
fscanf(ifp, "%lf", &auctions_prices[i]);
View 2 Replies
View Related
Mar 29, 2013
I plan to read one character at a time from a very large text file.
The tokens that I would like to extract are basically words and they are delimited by varying numbers of spaces or tabs.
For example, the first line has two words separated by five spaces:
Joe Mark
Problem:I apply get() each time advancing towards Mark. For me to know ive reached the next token, Mark, I merely have to get() until it equals 'M'. The problem by then its too late as Ive already captured 'M'. I somehow need to capture the fifth space and the code must "know" the next character is 'M' without getting() it.
Kernighan and Richie offer us a function getch() and ungetch() whereby the ungetch pushes back the last character into the input . The problem with this is that I believe is that the buffer is not the actual input buffer stream but rather a simulated character array which contains the input. I dont think itll be as efficient on a text file so large.
View 9 Replies
View Related
Mar 17, 2015
I am trying to generate possible combinations of 4 lower case letters from aaaa - zzzz. The words will then be used against a file that contains encrypted passwords that each relate to a 4 lower case word. I have successfully generated the words. Now all i need to do is put the created words (aaaa, aaab, etc...) to an array so I can use crypt((array[i+1]), buff) and print the encrypted 4 letters that come from the file. I am having a lot of trouble complete this though.
New problems that are occurring
The main problem is in the all function and main. I'm not wanting to print the aaaa, aaab, aaac, etc to the screen but I want to put it in an char array so that i can compare each individually to each line from the file using crypt. How to put all 456976 combinations into an array, compare it to each line of code, and print the solutions to the screen.
File:
$1$6gMKIopE$I.zkP2EvrXHDmApzYoV.B.
$1$pkMKIcvE$WQfqzTNmcQr7fqsNq7K2p0
$1$0lMKIuvE$7mOnlu6RZ/cUFRBidK7PK.
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
[Code]....
View 1 Replies
View Related
Mar 13, 2013
how to read characters from user and construct a sting and then extract part of it using substring?
View 2 Replies
View Related
Jan 5, 2015
I have got to create a program that will gather input from the user and then encrypt & decrypt those characters.
I'm not very confident at coding so I'm sure many parts of my code are written poorly and not following the best practice so I have written a simple version of an algorithm where the program simply adds/subtracts a value of 2 to/from the ASCII values but I have discovered the use of the rand() and srand functions but I'm unsure how to go about using them within both my encrypt and decrypt function as a single value (static variable?).
Here is my code in its entirety at the moment.
#include <iostream >
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
char getMenuSelection (char m);
void encrypt (char key[]);
void decrypt (char code[]);
[code]....
This code currently doesn't execute due to ' undefined reference to 'encrypt(char*) '
Question
I am asking how to generate the random number and incorporate this into my encrypt and decrypt functions.
1. Would the use of a static or global variable make this work as is?
2. Would I need to create separate class files for both functions?
View 2 Replies
View Related
Jan 10, 2014
I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.
Here is my full code:
/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */
#include<stdio.h>
/* Declarations*/
#define MAX_STRING_LEN 1000
int count_characters(char S1[]);
int main() {
[Code] .....
So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:
Code:
#include <stdio.h>
/* counts character of a string*/
main() {
int nc = 0;
int c;
for (nc = 0; (c = getchar()) != '
'; ++nc);
printf("Number of characters = %d
", nc);
}
which works...
View 1 Replies
View Related
Nov 24, 2014
I have trouble printing out the movies starting with inputing only two letters.
#include <iostream> //cout, cin, endl,getline
#include <fstream> //ifstream
#include <cstdlib> // exit
using namespace std;
const int NUM_MOVIES = 116792;
struct Movie {
[Code] ....
View 1 Replies
View Related
Jul 19, 2013
How do I prevent user from entering characters or symbols instead of numbers?
int num;
cout<<"Enter a number."<<endl;
cin>>num;
Also, how do I prevent user from entering a number instead of a character?
char c;
cout<<"Enter a character."<<endl;
cin>>c;
View 9 Replies
View Related
Feb 7, 2013
I am stuck in this program, Be given a string of chars, where each single char belongs to the following alphabet: a..zA..Z0..9 (So, in the string there are only lowercases, uppercases and digits. No blank, no comma, ...). For every char of the given alphabet, count how many times in the string
1-- the char belong to a sequence of identical chars whose length is at least three (i.e.: in the string cc74uyrpfccc348fhsjcccc3848djccccc484jd for three times the character 'c' satisfies this condition)
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
[Code]...
2-what is the longest substring of characters strictly rising interm of ASCII code(the following is greater (>) of the previous)
3- what is the longest substring of successive characters interm of given string rannge (i.e.: fhkjshdfruytyzABCDEfglsj => 7)
View 1 Replies
View Related
Mar 20, 2014
so my question is i want to print characters,no string just an array of characters,i do this but it s not working,maybe i have to put the '' at the end?
Code:
int main() {
int i;
char ch[5];
for(i = 0; i < 5; i++) {
scanf("%c",&ch[i]);
[Code]...
View 6 Replies
View Related
Jul 6, 2014
Im supposed to find the common characters between two string characters, assuming that the user wont input duplicate letters like ddog. When I run my code I get an output of a question mark upside down. Here is my code with comments on what each part is supposed to do
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str1[20], str2[20],remp = '