C++ string Manipulation

C++ string Manipulation

Page 1 of 1

5 Replies - 2395 Views - Last Post: 18 November 2010 - 04:42 AM Rate Topic: -----

#1 msqueen008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 10

C++ string Manipulation

Posted 15 November 2010 - 08:51 PM

I am new at C++ and trying to teach it to myself. I wrote the following code and it will not work for me. The first name is fine, but when I try to also accept a number using a function it will not work...can you tell me why? The number part of the program has to verify that the character is a number, character 0-9. If not a number it will appear as if you did not type anything looping for the next character entry.

#include <iostream> // cout
#include <conio.h> // getch
#include <string>
#include <cmath>

using namespace std;

// Globals
#define MAX_STRING 50

//Function Prototype
int numberString();

// funtion prototypes
int numberString (int x);

int main () {
     //locals
    char firstName[MAX_STRING]; // string first-namespace
    char key; // single-key entry
    int i; //counter

    cout << "Please type your first name:  ";
    i = 0;
    do{
        key = getch(); // take the character, but do not print to console
        cout << flush; //clear input stream buffer

        if (key == char(224) || key == char(96)) { // ignores 1rst byte of special 2 byte special keys
            key = getch(); // get second byte
            cout << flush;
        }
        if (key == char (13)) // if enter key
        break; // jump out of loop
        if (key == char(8) && i > 0) {
            cout << "\b \b";
            i--;
        }
        if ((key >= 65 && key <= 90) || (key >= 97 && key <= 122)) { // if key is upper or lower alpha-char
            if (i== 0) { //if 1rst character in string
                key = toupper(key); // then make uppercase
            }
            cout << key; //echo print character back to console screen
            firstName[i] = key; // append to string
            i++; // increment to next character in string
        }
    } while (i < 49);
    firstName[i] = NULL;

    cout << endl << "\nFirst Name:  " << firstName << endl;

}

int numberString(){
    //LOCALS
    int number[30];
    char key;
    int count;

    do  {
       key = getche();
       cout << flush;

       if (key == char(224) || key == char(96)) {
           key = getch();
           cout << flush;
       }
       if (key == char(13))
       break;
       if (key == char(8) && count > 0) {
           cout << "\b \b";
           count--;

       }
       if (key > 48 && key < 57)
       cout << key;
       number[count] = key;
        count++;

   } while (count < 49 );
   number[count] = NULL;

   cout << endl << "\nNumber:  "  << count << endl;

   return 0;

}  // function main




Would really appreciate the help..I know I probably messed the whole code up..
thank you
msqueen008

This post has been edited by no2pencil: 15 November 2010 - 08:56 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: C++ string Manipulation

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: C++ string Manipulation

Posted 15 November 2010 - 09:03 PM

Welcome to DIC!

Please edit your posting (see the "EDIT" button in lower right). (If Edit is not available make a new posting on this thread and skip ( a ) below.)
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.


Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?

If you are teaching yourself then immediately go to your code and remove the conio.h library and never use it from this point forward. It is a bucket of nastiness that you should not get into the habit of using. Use the C++ standard libraries instead.
You don't need to use getch() when getc() and getchar() are available in the standard language.
http://www.cplusplus...ry/cstdio/getc/
http://www.cplusplus...cstdio/getchar/
Was This Post Helpful? 1
  • +
  • -

#3 msqueen008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 10

Re: C++ string Manipulation

Posted 17 November 2010 - 10:27 PM

( a ) Does your code compile? yes the program compiles

( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are. There were no errors when it compiled

( c ) Is the program producing any output? yes, when I run the program it ask me for my first name and returns my first name, but it is not let me type a number and does not return anything. It like skipps the whole second function.

( d ) How is the actual output different to what you want / expect? I expect it to ask me for my first name, return my first name i typed, ask me for a number, and return the number i typed. The numbers have to be numbers. If I type a letter it rejects it.

Give details and, ideally, examples.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it? I tried adding a datatype to the function int numberString, but it still will not work.

thank you for your quick response.
Was This Post Helpful? 0
  • +
  • -

#4 seeP+  Icon User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: C++ string Manipulation

Posted 17 November 2010 - 10:38 PM

Excuse me I just took a second look. Call the number function from main and define the function outside of main

This post has been edited by seeP+: 17 November 2010 - 10:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 msqueen008  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-November 10

Re: C++ string Manipulation

Posted 18 November 2010 - 01:05 AM

thank you for your help my code worked..greatly appreciated
:)
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: C++ string Manipulation

Posted 18 November 2010 - 04:42 AM

Please don't use titles like "Need Help". We KNOW you need help, it's WHY you came here! Use a title descriptive of the topic you're posting about.

Modified title to reflect content.

This post has been edited by JackOfAllTrades: 18 November 2010 - 06:12 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1