Newb Help needed.

This is a discussion on Newb Help needed. within the C++ Programming forums, part of the General Programming Boards category; I've been learning from the book C++ without Fear (first edition) and lost my cd so I have no way ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Newb Help needed.

    I've been learning from the book C++ without Fear (first edition) and lost my cd so I have no way of checking how to do the exercise properly.I am having trouble with exercise 7.3.2. The idea is that it converts string to uppercase and the exercise is about including pointers. Problem is, it adds /n to the front of the inputted text and doesn't uppercase the first letter.
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string.h>
    #include <ctype.h>
    using namespace std;
    
    
    void convert_to_upper (char *p);
    
    
    int main() {
        char s[100];
    
    
        cout << "Enter string to convert and press ENTER: ";
        cin.getline(s, 99);
    
    
        convert_to_upper(s);
        cout << "The converted string is: /n";
        cout << s;
        cin.getline(s,99);
        return 0;
    }
    
    
    void convert_to_upper (char *p) {
        while (*p++)
            *p = toupper(*p);
    }
    Thanks in advance :D
    Last edited by Arthur Popof; 01-31-2012 at 11:17 AM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You're incrementing p before you use it in the toupper call. Try
    Code:
    for ( ; *p; p++)
        *p = toupper(*p);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    > cout << "The converted string is: /n";
    Perhaps you were going for a newline, which would be
    cout << "The converted string is: \n";

    Note the way the / should lean the other way \
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Yep and yep, thanks!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    I'd advise you to learn about std::string and std::getline, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The newb has a newb request
    By Pancakes in forum C Programming
    Replies: 15
    Last Post: 05-27-2011, 09:04 PM
  2. newb help
    By ke121885 in forum C Programming
    Replies: 5
    Last Post: 10-02-2009, 10:53 AM
  3. newb help needed
    By bsnimunf in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2005, 06:55 PM
  4. Im a newb help!
    By Siggy in forum C++ Programming
    Replies: 27
    Last Post: 06-30-2004, 08:35 PM
  5. Please help out a newb
    By Marcos in forum Game Programming
    Replies: 7
    Last Post: 02-02-2003, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21