C++ :: Formatting User Input And Check How Many Words Are In String

Feb 5, 2013

I'm trying to write a short program that takes the input from a user and trims any leading/trailing white space, and then checks how many words are in the string. Problem is, I'm only allowed to use stdio.h and stdlib.h. How can I accomplish this? Also, how would I check if any of the characters which were entered aren't either a number, letter, or '-'?

View 3 Replies


ADVERTISEMENT

C :: Generate Words From User Input (Phone Number)

Aug 13, 2013

The basic idea of this exercise is to generate words from a user-input, seven-digit number(a phone number). The code runs, but for some reason, I can't get it to print the numbers into the file.

Code:
/*Write a program that will generate a word based on a randomly generated, seven-digit number. there should be 2187 possible words. Avoid "phone numbers" that begin with 0 or 1*/

#include<stdio.h>
#define PHONE 7
void wordGenerator(int number[]);
void wordGenerator(int number[]) {
//loop counters for each digit in the number

[Code] ....

View 3 Replies View Related

C++ :: Stringstream - Split User Input Into Words Separated By Space

Apr 20, 2014

I trying to get input from the user and split into words that separated by a space.

string s = "1 2 3";
istringstream iss(s);
int n;
while (iss >> n) {
cout << "* " << n << endl;
}

The code above works fine but i want to get the string from user. the code below only prints the first word and trashes rest of the words in the sentence.

string s ;
cin>>s;
istringstream iss(s);
string n;
while (iss >> n) {
cout << "* " << n << endl;
}

View 2 Replies View Related

C++ :: How To Improve Validity Check For User Input

Apr 24, 2013

I wonder how can I improve my validity check for user input? I only want them to key in certain range of digit.

Also for my validity check, when I key in character such as ABC, it lead to infinity loop

Here is my code : Code: /*Write a program that can calculate user's age by getting user input their birth date.*/

#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int yyyy, mm, dd; //year, month, day
int i = 0; //for the sake of validity check

[Code]...

View 3 Replies View Related

C++ :: How To Recognize Two Words Entered By User For A String

Feb 11, 2013

In my little experiment i am trying to get the compiler to recognize 1 word with 2 parts from a list of names.

For example: if the user wanted to choose "candy bar", from a list of items which include: "candy bar", "cat", "dog"

My current code can recognize words without a space like cat and dog. But how can i recognize candy bar?

I tried using getline but its of no use.

View 2 Replies View Related

C++ :: How To Error Check If User Input Is Letters And Not Numbers

Jun 9, 2014

How do I error check if the user is inputting letters and not numbers? For example, if the user inputs "Lab.txt" I need to display an error message. If they input "Lab2part2.txt" then this is correct and what I want.

I've found a lot of information online on how to error check for numbers or a single letter (EX: 1,2,3, etc. or 'A' 'B' 'C') but nothing for actual WORDS or maybe I should refer to it as a string of characters?

Is there any way to do this? Because my program requires I ask the user to input the name of the file. But the way my code is currently set up is even when the user inputs the wrong file name it still opens the file. I want to prevent this from happening so my thought was to error check user input.

/*Program to determine company's weekly payroll*/

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void OpenTheFile() {
ifstream inputFile;
string filename;
char letter;
int number;

[Code] .....

View 1 Replies View Related

C :: Check To See If String From Standard Input Is Alphanumeric Or Not?

Dec 25, 2014

I'm trying to check to see if a string from standard input is alphanumeric or not. I feel as though there may be a function for this but...

Code:
for(test_pass = i; test_pass < i; i++){
if(test_pass[i] == ('1', '2', '3', '4', '5', '6', '7', '8', '9', '0'))
printf("Your password is alphanumeric!
");
}

The compiler wasn't too happy with this.

View 10 Replies View Related

C :: Compare String User Input With A String In Binary

Jul 14, 2014

I have problem with string compare. I want to compare the string user input with a string in binary. And I don't know how to do it. Problem in function login();Here is the code: And you also can download file in attachment too..

Code:

#include<conio.h>#include<dos.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
char nsb=1;
char ch, password[20], passlogin[20], inputpass[20], checked[20];
FILE *fp;
}

[code]....

View 1 Replies View Related

C :: Read In N / Then N Words And Check To See If First Word Is Repeated

Sep 23, 2013

Read in n, then n lastnames, and check to see if the first in the list is ever repeated again.Here's what I have so far:

Code:

#include <stdio.h>
#include <stdlib.h>
int strcmp(char *w1, char *w2);
int main() {
int j, n;
char string1[30], string2[30];
}

[code]....

I see the problem is that it lies within n amount of string that I am not going through every string to compare to the first one.How will do compare every string to the first one?

Current output:
3
alex
alex
alex
Not repeated

3
alex
ash
peter
Not repeated

View 2 Replies View Related

C++ :: Program That Will Allow A User To Input Their First Name Into A String

Feb 23, 2015

So we have a weekend assignment that is "Write a c++ program that will allow a user to input their first name into a string, and use the switch/case statement to produce the following output. Your Program should prompt the use rwith the numbers and the options for each (school, classification, and mood) allowing them to make a choice"

My issue is with the output. The output at the end is the number that they input and not the name of the case they chose, so if they chose option 1 for school it does not print out "BRCC" at the end for the output it prints a 1.

#include <iostream>
#include <string>
using namespace std;
int main(void) {
string name;
cout << "Please enter your name: ";
cin >> name;

[code].....

View 7 Replies View Related

C++ :: Age Is Int Variable And Name Is A String - User Input

Jun 18, 2013

I have a problem that states: age is an int variable and name is a string.

What are the values of age and name after the following input statements execute.

cin << age;
getline(cin, name);

if the input is:

a. 23 Lance Grant

b. 23 Lance Grant

Ok I have been fooling around with this book all day. My instructer wants us to build the program instead of just saying

a = 23 Lance Grant

How to input B. I mean when I write the program I do not know how to ask for two lines for one input.

View 1 Replies View Related

C++ :: Capitalize Every Other Word In A User Input String

Nov 14, 2014

I've been agonizing over this all day. The assignment is to capitalize every other word in a user input string.

My logic is as follows:

1. I have the program get each character until it encounters white space (using for loop)
2. Then it should capitalize each character after the white space until it encounters another white space (using while loop).

My problem though is when i try to create a condition for while loop i have to terminate is when white space is encountered, but the very first character to start the while loop is a white space..

I was told to define boolean values, but after trying a few boolean conditions I'm still stuck..

Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

[Code] .....

View 5 Replies View Related

C++ :: Counting Vowels In User Input String

Feb 11, 2013

I need to write a program using at least one while loop to count and display the amount of vowels in a user input string. This is what I have so far.

#include <iostream>
#include <cmath>
using namespace std;
int main() {
int acounter(0); // Create counters for each vowel

[Code] ....

View 2 Replies View Related

C++ :: Inserting User-input String To Array

Nov 8, 2014

I'm trying to code the program that will store item data.And I'm having problems to receive user-input string to array.

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <iomanip>
using namespace std;

[code].....

View 6 Replies View Related

C++ :: Storing Information In String Without User Input?

Oct 9, 2014

Is it possible to store information in a string without user input? If so, what code would I use for it?

View 2 Replies View Related

C++ :: String Array - Sorting User Input

Oct 25, 2013

I have been reading up on arrays and string array. I created a string string text[0] and it is defined by user input. I am trying to sort the input. I want Michael to read Macehil.

When I wasn't using an array and just a string I did this:

return sort(text.begin(), text.end()); a

And it worked fine. Do I need to change my string into a char? If so, would I static cast that?

View 9 Replies View Related

C++ :: Converting User Input Int To String Output?

Apr 22, 2014

I am writing a code in a class that will change the user input which is integer to a string. For example if the user enter 13347..the output should change to "one three three four seven" on the user screen.I'm not getting the right output.

Code below:

#include <iostream>
#include <string>
using namespace std;
string arr[]={"one","two","three","four","five","six","seven","eight","nine"};
class convertTo{
public:
int signed num;
int convet(){
cout<<"Enter a number to convert to string "; cin>>num;

[code]....

View 2 Replies View Related

C/C++ :: Remove Vowels From User Input String

Aug 13, 2013

how to remove the vowels from the user input.?

View 4 Replies View Related

C :: Program That Allow User To Input String Then Scans The File

Sep 21, 2014

This program will allow the user to input string then scans the file if it contains the same string given by the user. But i always get "MATCHED" even if i enter random string. I tried and tried to place the if statement in different positions but i dont get my expected output.

Code:
#include<stdio.h>#include<stdlib.h>
#include<time.h>
#include<string.h>
int main() {
int found;

[Code] ....

View 6 Replies View Related

C :: Read Characters From User Input And Construct A String

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

C++ :: Search In A Vector Array From User Input String?

Sep 30, 2013

How would you search in a vector array from a user input string?

ex: user input : "Hello"

output: search vector array and find the line that has the string "Hello" and output the array "Hello" is on?

View 1 Replies View Related

C++ :: Take User Input As String And Test To See If Matches A Word

Dec 4, 2014

I'm creating a program that takes user input in the form of a string and tests to see if it matches a word. Each correct word will increase their score by one. Here is a portion of the code that is not working.

...
else if(s == 32) {
if(!currentLetter.empty()) {
currentLetter.erase(currentLetter.length() - 2, currentLetter.length() - 1);
} if(currentLetter.compare(oWord) == 0) {

[Code] .....

To me, this looks like it should do a very simple task as intended-take a String, compare it to another, and reset the word if they match or output incorrect if not. But, I'm not sure if there is some quirk in C++ with Strings, because this code always outputs Incorrect. Please try again. and the score never increases. I also tested this by literally setting the strings equal in the code, which still resulted in it not doing what it's supposed to.

View 3 Replies View Related

C++ :: Cannot Search Words From Input

Apr 14, 2013

i am trying to create a c++ program that asks for an input and determines if it is a question or not by searching each word from the input across a database (txtfile)..so far I have managed to make it search the database for the word but it only works on one word.... i would like to be able for it to search every word on the input separatly..here is the code:

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[code]....

View 4 Replies View Related

Visual C++ :: Vector Input / Counting Words

Feb 3, 2014

I am trying to write a program that counts specific words that a user inputs "Howdy/howdy/Whoop/whoop" (yes I go to Texas A&M, hence those specific words) I am having an issue where it wont count the first word even if it is "Howdy"

For example if I put it:

"Howdy howdy whoop Whoop" - it only outputs that it counted 3 words

now if I were to do:

"Hello Howdy howdy whoop Whoop" - it would count 4.

Code:

#include "std_lib_facilities_4.h"
int main(){
cout << "Please enter desired words, when you have entered all words, please type CTRL+d (EOF Command)
" << endl;
//It was assumed that EOF command was going to be used here hence the necessity of 'CTRL+d'
vector<string>words;

[Code] ...

View 2 Replies View Related

C++ :: How To Check User Selection (yes / No) On Message Box

Feb 10, 2013

How can you tell what button(yes/no) on a message box is clicked? Below is what I have right now but I am getting errors.

if (MessageBox(NULL,"The Message", "The Title", MB_YESNO) == IDYES){
do something
}

View 7 Replies View Related

C/C++ :: Program To Print Histogram Of Lengths Of Words In Its Input

Mar 4, 2014

Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.

See [URL] ....

I'm trying to do this exercise from K&R on my own (with my own code), but I'm receiving a signal (Illegal instruction (Core dumped)) when the input is too large.

#include <stdio.h>
#define MAXWORDLENGTH 10
int main(void) {
int c; /* Character read */
long length[MAXWORDLENGTH + 1];
int reading_word = 0;
int word_size = 0;

[Code] ....

Where the problem might be occurring. I tried debugging with GDB but found no useful information.

Program received signal SIGILL, Illegal instruction.
0x00007ffff7a3b76e in __libc_start_main (main=0x4005d4 <main>, argc=1,
ubp_av=0x7fffffffe2a9, init=<optimized out>, fini=<optimized out>,
rtld_fini=<optimized out>, stack_end=0x7fffffffe298) at libc-start.c:258
258libc-start.c: No such file or directory.

The program output is also wrong when tested with the code provided at the link given above.

View 7 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved