C++ :: Char Arrays - Getting User Input Through Different Methods
May 29, 2014
I've been experimenting with char arrays and getting user input through different methods.
int main() {
char userInput[21];
/*I understand that over here, a maximum of 20 letters can be input, and only letters before a space will be stored in userInput*/
std::cin >> userInput;
std::cout << userInput << std::endl;
[Code] ....
As I was testing, whenever I would input a single word for userInput (for example "hi"), the program would work as expected: it would output "hi" and I'd be able to input a sentence of sorts for userInput2 (for example "hello world") and have it outputted.
But if I were to input more than one word for user Input (for example "hi how are you"), the program would output "hi" as expected, but it wouldn't let me input anything for userInput2 and would just output the rest of the first input; in this case, "how are you" would be outputted and the program would end. I am not aware of the logic error at play.
View 7 Replies
ADVERTISEMENT
Sep 9, 2014
I'm trying to write a program that takes input from the user (thats a char) and outputs it to the monitor in hex form.The program is meant to continuously take input from the user then output to the monitor in hex form until an EOF is detected this triggers the program to close.The following code does this except that I get a lower case 'a' at the end of each output.I think the 'a' has to do with the enter key and if that is the case how can i tell the program to ignore this input from the user.
Example: input from user: ABC output to monitor: 41 42 43 a
Code:
#include <stdio.h>
int main(void) {
char myChar;
while(EOF != (myChar = getchar())) {
printf("%x ",myChar);
}
system("pause");
return 0;
}
View 5 Replies
View Related
Apr 27, 2014
So I'm trying to make a program, Which has nothing to do with the topic. But I ran into a problem.
I need to get a char array size of 6 doing:
char myChar[6];
but the size (6) is undefined until user input.
So I need to do char myChar[var]; (Var being 6 for now).
When I do:
char myChar[6];
It works!!!
But when I do:
int val = 6;
char myChar[val];
It doesn't work.
View 3 Replies
View Related
Feb 7, 2014
I'm trying to create a function where it allows the user to type in multiple amounts of integers, so if the user wanted to have 3 different storages that hold different integers, the input would look something like this:
5
97 12 31 2 1 //let's say this is held in variable "a"
1 3 284 3 8 // "b"
2 3 482 3 4 // "c"
2 3 4 2 3 // "d"
99 0 2 3 42 // "e"
Since we don't know what number the user will input every time, I'm not sure how to create a dynamically allocated array that will create an x amount of arrays every time.. I want to be able to access each index of a, b, c, d, e or however many arrays there are.
So far, this is what I have, but I'm having trouble creating the arrays since it's unpredictable. I'm purposely not using vectors because I don't really get how pointers work so I'm trying to play around with it.
int* x;
int length, numbers;
cin >> length;
x = new int[length]
for (int i=0;i<length;i++)
{
cin >> numbers; //this doesn't work because it only grabs the first line for some reason
x[i] = numbers
}
View 3 Replies
View Related
Nov 3, 2012
The following program takes user input into two arrays and should then determine how many items are different by comparing them with a loop. The comparison always show zero correct answers.
#include <iostream>
using namespace std;
const int QUESTION = 20;
//class for testing grades
class TestGrader {
[Code] ....
View 4 Replies
View Related
Sep 29, 2014
I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.
/* this is my terminal readout
joe bob
<- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit
[Code]....
View 2 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
Dec 29, 2013
lets say I have a char array with four elements but only one char is used, does it write four elements or just one?
View 3 Replies
View Related
Sep 14, 2013
I want to compare alphabetically two arrays. I try this:
char a[10] = "AABC";
char b[10] = "ZABC";
if(a > b) cout << a;
else cout << b;
But it always outputs a. How can I compare char arrays?
View 3 Replies
View Related
Jun 8, 2012
I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:
Code:
#include <string>
#include <iostream>
#include <cstring>
template<typename T1, typename T2>
bool compare(const T1& lhs, const T2& rhs) {
[Code] ....
View 4 Replies
View Related
Mar 21, 2013
how to correctly use realloc on an array of char arrays? Say I want to store strings in arrays (=array of char arrays) and double the size of max. strings (y value):
Code:
int x=200;
int y=10;
char *carray[y];
for (int j = 0; j < y; ++j)
carray [j] = malloc (sizeof(char)*x);}
}
[code]...
fix the realloc part of my code?
View 2 Replies
View Related
Mar 30, 2013
I have most of the code working properly, but I'm having trouble with a certain area. Currently I have multiple 2D arrays. One is a char array and the other is an int array. In the int array I have to find the max number in each column, which I've done. The problem is, I need to print the max number's row in relation to the char array's row.
For example,
Code: int array[2][3] = {60 50 30 0 100 1}
The max numbers are 60, 100, 30.
char array[2][length+1] = {nameOne nameTwo}
How it needs to print:
nameOne has max score of 60.
nameTwo has max score of 100.
nameOne has max score of 30.
I just can't understand how to compare the two arrays in the right way, so it'll know that nameOne is associated with the numbers in row 0 and nameTwo in row 1, etc.
View 1 Replies
View Related
Nov 21, 2013
send(sConnect, (userinput, key), 256, 0);
sConnect is already predefined as a socket
Key is a character array of [32]
Userinput is a character array of [256]
How can you send both of them at the same time using the send function without having to create a separate connection?
View 1 Replies
View Related
Nov 16, 2013
I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
Here's my code:
Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph
[Code] ....
View 4 Replies
View Related
Apr 6, 2014
This program works as expected:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
unsigned long long int address;
float current;
unsigned char pressure_units;
}
[code]....
But I don't like how I had to use malloc and free. Is there a different way to accomplish copying the string into a char pointer without resorting to dynamic memory allocation?
View 1 Replies
View Related
Mar 20, 2014
I've been trying to finish this airplane seating program I've been working on for a month or two. The problem I have right now is that if the user inputs a number when prompted to enter the column letter he wants, the program goes on to the next step as if the prompt had been answered correctly.
So I originally used string but I heard from Daleth that Char was better, but I don't know how to apply it. I figured out cin.fail, but I've yet to finish this part.
View 2 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
Sep 16, 2013
I'm extremely rusty at C but is this the best way to store an input string into a char*?
Code:
int length = 100; //initial size Code: char * name = malloc(length * sizeof(char)); //allocate mem for 100 chars
int count = 0; //to keep track of how many chars have been used
char c; // to store the current char
while((c = getchar()) != '
'){ //keep reading until a newline
if(count >= length)
name = realloc(name, (length += 10) * sizeof(char)); //add room for 10 more chars
name[count++] = c
}
Is this a good way and what could be better?
View 2 Replies
View Related
Jan 23, 2015
Basically I'm having trouble reading more than one character.I want the user to input character traits(ie, w, a), if two traits are entered they get the job.
But I cannot get my program to read more than one character input.
This is what I have so far...
#include <iostream>
using namespace std;
int main(){
char input;
int count = 0;
[Code] .....
View 6 Replies
View Related
Feb 8, 2015
I am writing a program that asks the user for their gender. I know that atoi converts arrays into integers, but I need the input from the user in the form of F or M into a char, and return it to the main function to be displayed at the end of the program.
string Employee::getgender(char gen)
{
cout << "Please enter your Gender: " << endl;
//atoi function would go here, what for char?
getline(cin,gen)
return gen;
}
View 1 Replies
View Related
Oct 27, 2013
I'm trying to write a code that takes two arrays from the user (presumably in ascending order) and then passes the sizes of both arrays and a pointer to each to a separate "int* mergeArrays" function that will merge sort the two. I've written a lot of the code, but I can't get it to compile. I get errors: lab6.c: In function "main":
lab6.c:31:14: error: expected expression before "int"
mergeArrays(int* firstArray, int size1, int* secondArray, int size2);
^
lab6.c:31:14: error: too few arguments to function "mergeArrays"
[Code] ....
View 2 Replies
View Related
Sep 23, 2013
I am having trouble with file I/O.
Let say I have an input file like this
x1 y2 b4
85 z4 x2
I'm trying to read the input without using arrays. I would use fscanf(*arguments*) to get the first char, which is x. Then I call it again to get the second one, which is 1. Now I would need to get the second input, y2. How do I skip the white space character to get to char y ? And is it an effective way to get all inputs though ? (each input, example x1, is read into a function).
View 3 Replies
View Related
Apr 17, 2014
STRING s1 = “FOOBAR”
Here STRING is a user defined class. how to assign a constant char array "FOOBAR" to string object? Copy constructor need to be same class type as parameter. and overloading assignment operator also need to be same class type. I think 'friend' can let pass another type of object rather than STRING to do operation on overloaded '=' operator. How could it be done if it is possible at all?
View 2 Replies
View Related
Nov 13, 2014
So my question was:
Write a program to read in a sequence of characters one by one. Print out the characters in reverse. You should use a char[]. (Remember single quotes are used for char)
For example:
Please enter characters one by one: (Enter 0 to exit)
h
e
l
l
o
0
You entered: hello.
The reverse of that is olleh.
and this is currently my code
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cmath>
using namespace std;
int main() {
char entry[20];
[code]....
im just not sure how to set that value and still make the for loops work
View 1 Replies
View Related
Sep 24, 2014
I want to take string input into a char array.
What is the functionality for the above problem.
View 1 Replies
View Related
Jun 14, 2013
i'm trying to write a calculator. the user will enter the terms and they will be stored in arrays. i will be storing those terms in string arrays and then later turn them to double to work with them. but the user should be able to enter terms like sin(252) and the program should be able to recognize it and calculate it.
View 4 Replies
View Related