3 Replies - 1310 Views - Last Post: 02 July 2012 - 06:21 AM Rate Topic: -----

#1 soucont   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-July 12

Help fix my code - garbage in my output C++

Posted 01 July 2012 - 07:44 PM

Hello everyone, I'm new and this is my first post on here so please don't rip my head off ;p

Basically it's sunday night and this is my homework that I've been working on. My output works, but there is some garbage in it. I needed to write a program that would take a string, print it backwards and convert uppercase to lowercase and vice versa. I am getting some errors when I compile but I am a newb programmer and don't know how to fix this. The warning "'strrev': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _strrev."

Any help you guys could provide would be greatly appreciated!




#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype> // not sure if I need this but just incase
using namespace std;

int main()
 {
 char str[20];// define array for letters in a word entered  
 int i;
 
 cout << "Please enter a word of no more than 20 letters: ";
 
cin >> str;;
 // uses ascii codes of the alphabet to change case
for(i=0;i<=strlen(str);i++){
 
if(str[i]>=97&&str[i]<=122)
 {
 str[i]=str[i]-32;
 }
 else
 str[i]=str[i]+32;
 }
 cout <<" Conversion is done\n";
 
cout <<("\n",strrev(str));
 return 0;
 } 


Is This A Good Question/Topic? 0
  • +

Replies To: Help fix my code - garbage in my output C++

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Help fix my code - garbage in my output C++

Posted 01 July 2012 - 08:13 PM

Decide whether or not you're using C strings or C++ strings.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Help fix my code - garbage in my output C++

Posted 02 July 2012 - 05:58 AM

Since you are using C-strings you need to insure that this string is properly terminated. Also normally you don't use the operator<= in for loops usually you would use just the operator< to insure that you don't exceed the bounds of your array.

To properly terminate a C-string you must insure that after the last valid character there is a end of string character '/0'.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 soucont   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 01-July 12

Re: Help fix my code - garbage in my output C++

Posted 02 July 2012 - 06:21 AM

View Postjimblumberg, on 02 July 2012 - 05:58 AM, said:

Since you are using C-strings you need to insure that this string is properly terminated. Also normally you don't use the operator<= in for loops usually you would use just the operator< to insure that you don't exceed the bounds of your array.

To properly terminate a C-string you must insure that after the last valid character there is a end of string character '/0'.

Jim



Thanks Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1