C++ :: How To Store User Input In Character Array Through Getline
Jul 2, 2013
int main() {
char StudentName[4][10] = { "Hermine", "Paul", "Gertrude", "Leon" };
cout << "Student Names";
cout << "
Student 1: " << StudentName[0];
[Code] ....
This code work fine...but i want to take name from user..how i store it in character array through getline()..
View 6 Replies
ADVERTISEMENT
Apr 29, 2012
Question simple like that: Let user enter some words from keyboard, one word per line until a '.' (period) entered then print out result, for example:
Code:
Enter a word: word1
Enter a word: word2
Enter a word: .
You have entered 2 word(s):
word1
word2
Problem is I don't know how to declare the array of character at the beginning since I don't know neither how many word user may enter nor how many letter of each word. So I go ahead and declared like this: char word[20][50] but I know it is not best way.
Code:
int main () {
char word[20][50]; //array has maximum 20 words, each word maximum 50 character
int i=0, number_of_word;
do {
printf ("Enter a word: ");
[Code] ....
View 3 Replies
View Related
Jan 10, 2015
I'm coding a hangman game. I'm trying to store user entries so i can output them to show the user what they have already entered. Problem is that it's not display anything at all.
I'm trying to store multiple characters of course, and then display all characters stored.
char guess[27]={0};
cin >> guess[26];
int hit=0;
for(int i=0; i<len; i++) {
if( guess[26] == hidden_word[i] ) {
hit++;
select_word[i] = guess[26];
if(strcmp(hidden_word,select_word) == 0) {
[code]....
EDIT: I also get the error - Stack around the variable 'guess' was corrupted. At the end of the game.
View 6 Replies
View Related
Mar 7, 2015
I'm writing a simple text adventure style program in order to practice C++. I originally used char to get whether the user wanted to go north, south etc, requiring they enter W, A, S, or D, and then I used a switch statement, rather than a bunch of else if blocks to move in the appropriate direction. Last night I started updating the code and wanted to require the user type an actual string like "go north" to move, so I'm in the process of converting it over to getline();.
The way it works is the user will input "go north", and if the string is equal to "go north" it will set a variable to 1, 2, 3, or 4, so I can keep the switch statement. The program compiles but whenever it asks for user input, it always hangs waiting for 2 separate lines of input regardless of whether or not you put a space between your command. The move function is as follows:
void movement() {
bool loop = true;
int roomNorth = 0;
int roomWest = 0;
int roomSouth = 0;
int roomEast = 0;
int dirOption;
string dir = "";
[code]....
I read that it could be an issue with getline and the new line characters, which is why I added the cin.ignore in there...so yea the code will wait for 2 separate lines of input and then always outputs "Invalid direction!" regardless of the input.
View 3 Replies
View Related
Mar 26, 2014
I'm trying to make a c++ program of this but i don't know how to use the bubble sorting.
Write a C++ application that asks the user to enter 10 numbers. The program then stores those numbers in an Array. The program should display the Mean , Median, and Mode.
Mean is the average of the 10 numbers.
Median is the average of the 5th and the 6th numbers. (But you have to arrange the numbers in ascending order first using Bubble Sort before calculating the Median.)
Mode is the most frequent number among the 10 numbers.
View 5 Replies
View Related
Jun 14, 2013
The program should store a character array in reverse order then display the reversed array. I have also included in the code that will display the actual characters into the array as it loops through. So I know the characters are being stored, but why doesn't it display the entire string when I call it?
Code:
#include<stdio.h>
int main(){
char str[50];
char rev[50];
printf("Enter Desired Text: ");
scanf ("%[^
[Code] ....
Is it just my compiler?
View 5 Replies
View Related
May 28, 2013
write a program that ask his or her name and store it in a character array
View 1 Replies
View Related
May 4, 2013
I can`t seem to store multiple values into my vector when a user keys in an input. Lets say a user inputs 1,2 as the first set and later keys in 3,4.
My vector gets overridden by 3 and 4. I'm trying to pass these values into a function to store them into a vector and use them in the function.
Below is my code snippet
int reconstructSecret(int x, int y) {
int getX,getY;
getX=x;
getY=y;
vector<int> myXCordVec;
vector<int> myYCordVec;
[Code] .....
View 6 Replies
View Related
Jan 2, 2014
I am trying to store a large text file into an array so I can count the number of characters.
View 2 Replies
View Related
Mar 17, 2013
I am having problems printing "->" on the beginning of each line, im trying to do it as a loop and ending it when the user types "q". also i would like to know how to ignore text from the user when the input begings with a specific character. heres what ive done so far, and not currrently working as expected.
Code:
#include <stdio.h>
int main (void) {
char prompt;
printf("~~~ FRACTION CALCULATOR ~~~
[Code] ....
View 1 Replies
View Related
Apr 6, 2014
My program is designed to read input from the user and then store that input in a structure variable. Whenever i use the cin.getline() function, it comes up with a semantic issue. Here is the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int DESCRIPTION_SIZE = 100, DATE_SIZE = 20;
struct inventory {
string description[DESCRIPTION_SIZE];
[Code] .....
I left out the other functions. The error reads "no matching member function for call to 'getline'.
View 6 Replies
View Related
Mar 15, 2015
I'm trying to write a function in C that calculates the average test score given by the user an arbitrary number of times...
View 14 Replies
View Related
Nov 8, 2013
This program works but i need to build an additional loop that will re-prompt a user if his initial entry (int y_n) is other than 'Y', 'y', 'N', 'n'. For instance if a user tries to enter 'Yup', he will be prompted "That isn't a valid entry" and then re-asked to enter int y_n.
Note: If user answers Y he is asked to enter a value that is entered into an array. If the user at any point answers N, the program ends and final stats are cout.
#include <iostream>
using namespace std;
int main(){
char y_n;
int i = 0;
float input_value;
float myarray[100];
[Code] .....
View 1 Replies
View Related
May 7, 2013
So I have an array of my own class, "Restaurant," but I'm having a hard time figuring out how to store the info entered by the user into the array.
In total, I have two classes. I will show you all I have, it's a lot but I know it will be easier to understand.
My problem is in the AddRestaurant(), since that is where I should ask the user for all the information and store it in the array.
Also consider that I have to use the FTime class as well to handle the time. Basically I want to do something like this:
info[count].SetRating();
which will put the rating part of the class Restaurant into the array[count]
Also, my program doesn't compile because of the AddRestaurant and PrintAll functions since what I am showing is what I want to do, not what works.
//main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "Restaurant.h"
#include "FTime.h"
int ReadMenuChoice();
[Code] .....
View 4 Replies
View Related
Feb 25, 2013
I want to compare the part of the character array with the scanned input. I've initialized the character array (colourCompare).
What I want to do is, if the input colour matches up with one of the elements in the colourCompare array, it will then read the next value(I did not include "read the next value part"). If the input does not match up, then it goes back to the scanning part.
Code:
char colourCompare [12][6] = {"Black","Brown","Red","Orange","Yellow","Green","Blue","Violet","Gray","White","Gold","Silver"};
float resistanceCal() {
[Code]....
View 14 Replies
View Related
Mar 10, 2015
I'm trying to read the data from a file i/o and put them into an array of structs. But when I run the program, it gives me a bunch of "garbage" as an output. my code and see
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
[Code].....
View 7 Replies
View Related
Nov 1, 2014
I am making program that allows the user to determine how big the array size will be and then asks the user to make up numbers to fill the array. Every time run the program on Dev C++ it says "program has stopped working"
Heres My Code:
//Assignment 19 Program 2
#include <iostream>
using namespace std;
int main()
[Code].....
View 3 Replies
View Related
Feb 19, 2014
Write code to do the following:
1) declare a variable ptr as a pointer to int and initialize it to NULL
2) dynamically allocate memory for an array of 100 elements
3) read 100 elements from the standard input device and store them in the array.
This is what I have so far, I'd like to know if its ok or if something is wrong.
int *ptr = NULL;
ptr = new int[100];
cin >> dataPtr [arr];
View 2 Replies
View Related
Jun 6, 2014
Down on line 74, it does not wait cor an input, it simply says ...you failed.
#include <iostream>
#include <string>
#include <Windows.h>
#include <cstdlib>
using namespace std;
//prototyping--
void flater();
[Code] ....
View 6 Replies
View Related
Aug 10, 2012
How do I write an a program that will read an input file character by character?
View 1 Replies
View Related
Sep 15, 2014
I've been stuck on this for hours, and have scoured the internet for any insight on this particular issue with no luck.
I'll simplify the problem here. I have a lines in an input file:
John Smith 1234 Main St New York
Carol Thomas 5678 5th Ave Columbus
...
and I am reading into character arrays using code like this:
infile.getline(name,20);
infile.getline(street,15);
infile.getline(city,10);
but when I print the output using something like this:
outfile << "Owner Name: " << name << endl;
outfile << "Address: " << street << city << endl;
(let's pretend I included spaces between the address components, which I omitted to save space)
I get an output file of:
Owner Name: John Smith
Address:
The output stops after the name variable, which I believe is stored correctly. I don't think anything is storing in the address pieces, this is the problem.
I think it's because I'm not using getline() properly in this case. Can you string together multiple getline()s like that?
If this was my design, I'd read in strings instead, but that is not possible in this case.
View 6 Replies
View Related
Apr 25, 2014
I have written a program and i had to pass an array into a function and now i have a variable for the subscript of the array and i was the user to input data, specificlly a string, more specifically first and last name without having to create two arrays, i have to do this with other things in the program as well, here is part of the program:
int addFunc(const int totNum, string city[],string state[],string street[],string name[],
int addNum[],int zip[],int telNum[],double bal[],int dateLp[], int addCount, int accNum, int usNum)//function will add a new account {
int countAf = 1;
if (usNum < 20)
[Code] ....
it should be able to take firstname space lastname
View 5 Replies
View Related
Mar 7, 2014
My parameters are that the users input has to be from 1 to 9 and the same number can't be entered twice. How do i modify this code to make sure that the user did those things or an error message should appear that their input is invalid.
#include <iostream>
using namespace std;
int main() {
int square[3][3];
[Code] ......
View 6 Replies
View Related
Mar 28, 2013
Say you the user inputs x number of names and then is to put in x amount of values for each name. How would you display these values in a 2d array and be able to add the values for each row which will represent each name?
View 5 Replies
View Related
Mar 26, 2014
Were supposed to be able to sort an array which is user input, but the first number they input is supposed to be the length of the array, and everything after that is the actual array.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int size;
int list[80];
bool isSorted (const int list[], int size);
[code]....
I'm stuck on how I'm supposed to use the first digit of input for the array size.
View 6 Replies
View Related
Mar 19, 2014
I am compiling using Microsoft Visual C++ and I am trying to create a program that ask's the user for 10 numbers, and these numbers will have to be loaded into a one dimensional array of 10 elements. I read up online as well as my book and looked at sample programs, then created mine which was similar to the sample programs I looked at, but the sample ones compile , while I get C2059 and C2061 Syntax Errors. I am new to programming so I understand it could be one small thing but I after looking over it I am completely clueless.
Heres my program:
Code:
#include
<iostream>
int
main()
[Code] .....
My program is also attached
View 5 Replies
View Related