Where to start

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Where to start

Postby Hydrokr0n1k » Wed Apr 11, 2012 11:54 pm

Ok I am being asked to do a series of functions in one but its a little beyond my current abilities and tips on where I can find some of the functions pre-made would be helpful
1. Write a function middle that accepts a string and returns the middle character if there are an odd number of characters and the two middle characters if there are a even number of characters in the string.

2. Write a function repeat that accepts a string and a positive integer n and returns that string repeated n times. Thus repeat("fho", 3) would return the string "hohoho".

3. People who learned how to type before word processors often add two spaces after a period ending a sentence. Write a function singleSpaces that accepts a string and returns that string with all occurrences of two spaces after a "." into changed single spaces.)

4. Write a function countWords that accepts a string and returns an integer indicating the number of words in that string. A word is separated by one or more spaces.

5. Write a function passwordCheck that insures that a potential password has at least 12 characters including upper case, lower case, and punctuation . This function needs to call three separate bool functions isUpper, isLower, IsPunctuation.

6. Write a function reverse that accepts a string and returns its reverse.

7. Write another version of reverse that does not use any loops (for, while goto etc).

8. Write a function sumAscii that accepts a string and returns the sum of all the ascii values of all the characters in that string.

9. Write a function sumArrayAscii that accepts an array of strings and returns the sum of all the ascii values of all the characters in all the strings in that array. Note that this function should use sumAscii from problem.

10. Using the get() and put() iostream functions, read in a .txt version of Pride and Prejudice from http://www.gutenberg.org, apply the count word function to see how many words are in the fina, apply your sum ascii function, apply your single spaces, and write out the new string to a new file. Use the sample code in the text sampler that uses the get and put functions.
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby exomo » Fri Apr 13, 2012 1:59 am

I'm sure you can find some of the functions if you use google a little bit.
But you really should try to do it yourself. If you have tried and you can't solve one of them just ask a specific question. They are not very hard to solve. You just need some string methods like length(), find(), find_first_of(). Look here for a list of string members: http://www.cplusplus.com/reference/string/string/
For 7. you can use the reverse function from the algorithm header.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Where to start

Postby Hydrokr0n1k » Sun Apr 22, 2012 8:00 pm

ok this is what I have done now

Code: Select all
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;

// Part 1
//1. Find Number in Middle and return it
std::string middleCharacters(const std::string &str)
{
    if (str.length() <= 0) return ""; // For an empty string, return an empty string (customize this as desired)
    return str.substr((str.length() - 1) / 2, 2 - str.length() % 2);
}
// Part 3
//1. loop through the string looking for ".  "
//2. when ".  " is found, delete one of the spaces
//3. Repeat process until ".  " is not found. 

string forceSingleSpaces1 (string str) {
    size_t found(str.find(".  "));
    while (found !=string::npos){
        str.erase(found+1,1);
        found = str.find(".  ");
    }

    return str;
}

int main(){

    cout << forceSingleSpaces1("sentence1.  sentence2.  end.  ") << endl;

    return EXIT_SUCCESS;
}


I still need
CS 1181 Strings and Test File Problems


2. Write a function repeat that accepts a string and a positive integer n and returns that string repeated n times. Thus repeat("fho", 3) would return the string "hohoho".



4. Write a function countWords that accepts a string and returns an integer indicating the number of words in that string. A word is separated by one or more spaces.

5. Write a function passwordCheck that insures that a potential password has at least 12 characters including upper case, lower case, and punctuation . This function needs to call three separate bool functions isUpper, isLower, IsPunctuation.

6. Write a function reverse that accepts a string and returns its reverse.

7. Write another version of reverse that does not use any loops (for, while goto etc).

8. Write a function sumAscii that accepts a string and returns the sum of all the ascii values of all the characters in that string.

9. Write a function sumArrayAscii that accepts an array of strings and returns the sum of all the ascii values of all the characters in all the strings in that array. Note that this function should use sumAscii from problem.

10. Using the get() and put() iostream functions, read in a .txt version of Pride and Prejudice from http://www.gutenberg.org, apply the count word function to see how many words are in the fina, apply your sum ascii function, apply your single spaces, and write out the new string to a new file. Use the sample code in the text sampler that uses the get and put functions.

II have started to wright part 2 and 4 but 5 and 10 are killing me where do I start or should I find a premade template?
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby exomo » Mon Apr 23, 2012 12:25 am

I don't think you can find all of these premade. Perhaps someone has done some of the functions, but it's not too hard to do it yourself.

2. is really easy. Just a hints: loop and string concatenation
You could show what you have try and say what's the problem, why you can't solve it.

4. you can use find_first_of and find_first_not_of methods to look for spaces and non-spaces. Similar to your solution of 3. you can search through the string, but instead of changing it so that the pattern disappears you can just use the version of find_first(_not)_of() that accepts a parameter for the starting position. Just pass the position after the last found character and the find starts there.

I would suggest you just try it and when you can't get one post the code you have tried and why it doesn't work and I can show you what is wrong. But I still won't do the whole assignment for you.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Where to start

Postby Hydrokr0n1k » Tue Apr 24, 2012 4:23 pm

ok I have done all of them but 3 and I did several on my own :)

Code: Select all
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include<stdio.h>
#include <string.h>
using namespace std;
int doubledelete();
int numberwordsinstring();
int reversewloop();
int pass()
int main();
int reverse();
int asciistring();
int asciiarray();
int getput();

// Part 1
//1. Find Number in Middle and return it
std::string middleCharacters(const std::string &str)
{
    if (str.length() <= 0) return ""; // For an empty string, return an empty string (customize this as desired)
    return str.substr((str.length() - 1) / 2, 2 - str.length() % 2);
}

//Part 2 Repeater

std::string repeat( const std::string &word, int times ) {
   std::string result ;
   result.reserve(times*word.length()); // avoid repeated reallocation
   for ( int a = 0 ; a < times ; a++ )
      result += word ;
   return result ;
}

int main( ) {
   std::cout << repeat( "Ha" , 5 ) << std::endl ;
   return 0 ;
}

// Part 3
//   loop through the string looking for ".  "
//   when ".  " is found, delete one of the spaces
//   Repeat process until ".  " is not found. 

string forceSingleSpaces1 (string str) {
    size_t found(str.find(".  "));
    while (found !=string::npos){
        str.erase(found+1,1);
        found = str.find(".  ");
    }

    return str;
}

int doubledelete(){

    cout << forceSingleSpaces1("sentence1.  sentence2.  end.  ") << endl;

    return EXIT_SUCCESS;
}

// Part 4 number of words in string

int numberwordsinstring()
{
   int i, numspaces;
   char nextChar;
   string msg;

   numspaces=1;

   cout << "Type in a string\n";
   getline(cin, msg);

   // checks each character in the string
   for (i=0; i<int(msg.length()); i++)
   {
      nextChar = msg.at(i); // gets a character
      if (isspace(msg[i]))
         numspaces++;
   }
   cout << "\nThere are " << numspaces << " words in this string.";
   cin.ignore();
   return 0;
}
// Part 6 Reverse

char STOP[265];

void reverse(char* a)// error: initializing argument 1 of ‘int reverse(char*)’ [-fpermissive]

{
int c = strlen(a) - 1;
for (; c >= 0; c--)
{
cout << a[c];
}
}

int reversewloop()
{
cout<<"Please digit an input to be reversed: "<<endl;
cin.getline(STOP,265);

reverse(STOP); //error: invalid conversion from ‘char’ to ‘char*’
// [-fpermissive]

system("pause");
return 0;
}
//7. Reverse no loops


int complexReverseString(string userInput)
{
   string source(userInput);
   string target( source.rbegin(), source.rend() );
   cout << "The reversed string is " << target << endl;

   return 0;
}
int reverse()
{
   ifstream input;
   string forward;

   cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
   input.open("information.txt");
   cin >> forward;
   //reverseString(forward, findStringSize(forward)); a function I'm not actually calling
   input >> forward;
   complexReverseString(forward);
   
   input.close();

   system ("pause");

}
//8 Ascii
//C program to accept a string from user and
//display its ascii value and
//then display sum of all ascii value of strings


int asciistring() {

char String[100];
int Sum,Index;
Sum=0; //Sum is initially zero

printf("Enter the string:\n");
gets(String); //Accept String from User

for(Index=0;Index<strlen(String);Index++)
{
Sum+=(String[Index]); //Adds (the ASCII values of) the String characters.
}

printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value.

return 0;

}
//9 acsiiArraysum

//10 getput


my issues I have are with part 5 , 9 , 10 I have an example of the pass program I coded but 1 it doesnt work and two it is not to the specifications needed and on 9 I am having rouble getting it to run as well and 10 is a bug challenge but Ill post what I have for each

part 5 .

Code: Select all
   

int pass()
        cout << "\t\tEnter Username : "; //asking to enter username
      cin >> User;
      cout << "\t\tEnter Password : "; //asking to enter password
   do
   {
      ch = getch();
      if(sizeof(password) != 0)
      {
         if( ch == '\\b')
         {
            putch('\\b');
            password.erase(password.size() - 1, 1);
                                continue;


         }
         
      }
      else if(ch <= '9' && ch >= '0' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z') //If user enters 1-9, a-z, or A-Z, add it to the password and display an asterisk
      {
         password += ch;
         putch('*');
      }
      
   }
   while {ch == '\\r');


part 9

Code: Select all
int asciiarray()
using std::cout;
using std::endl;
int ASCIIsumOfString(string s){
int sum = 0;
for(int i=0; i<s.length(); i++){
    sum += s[i];
}
return sum;}


int ASCIIsumOfStringArray(string *s, int numberOfStrings){
    int sum = 0;
    for(int i=0; i<numberOfStrings; i++){
        sum += ASCIIsumOfString(s[i]);
    }
    return sum;
}


and part 10 I dunno how to integrate with the required pats of my previous code it needs to be integrated with part 3 , 4 , 8 as well shouldI use winsocks or what to load the text from offline and also I was told fstream is for when the file is on your computer should I just copy and paste the file into a string in the function?

Code: Select all

int getput()
{
   string ????;
   fstream streamObject("http://www.gutenberg.org/cache/epub/1342/pg1342.txt");
   fstream localFile("pap.txt");
   fstream newFile("new.txt");
   
   

   if(streamObject.fail())
      cout << "file failed to load." << endl;
   else
      cout << "file load was successful." << endl;
   
   if(localFile.fail())
      cout << "file2 failed to load." << endl;
   else
      cout << "file load was successful." << endl;
   
   while(!streamObject.eof())
   {
      streamObject.put(localFile.get());
   }

   streamObject.close();

   char single = localFile.get();
   while(!localFile.eof())
   {
      ???? += single;
      single = localFile.get();
      
   }
   localFile << newFile;

   localFile.close();

   asciistring();
   doubledelete();
   numberwordsinstring();





   return 0;

}



   return 0;

}


also am I calling the functions right in the begining?
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby exomo » Tue Apr 24, 2012 5:35 pm

Good, now that you have done some things on your own I can tell you what you are doing wrong :D

First you should separate input and testing from the functions you are supposed to implement. e.g. in number 4 the task is "4. Write a function countWords that accepts a string and returns an integer indicating the number of words in that string. A word is separated by one or more spaces."
Your function does not accept a string but has some input in it. And unfortunately counting the spaces is not enough. There might be multiple spaces between two words. You could store if the last character was a space or not and only count when there is a non-space character after a space character.

5. Again try to keep to the task. You only need a function that checks if a password is "valid", you don't need asterisk input. If you want to have masked input do it in a separate function that does not do any checking. Your function won't compile because there are { and } missing and the some variables are not declared. (I posted some code that does password input some days ago in another thread :D)

6., 7. And again. Accept a string and return a string. Not accept a char pointer (ok, in c this is a string, but it's not as nice as string) and return nothing. For 6 just replace the cout with a string append operation to create a reversed string and return that string. For 7 you already have the string you should return.

8. Let me guess, you didn't do this one yourself. Better don't use char arrays if you can avoid it. You can do pretty much the same with c++ strings. Oh and ... accept a string and return the sum. It's not just because I want to annoy you, but you need this function for 9. Even better you have a correct solution for 8. in your code for 9.

9. The code looks good, just delete the first 3 lines and it should work. You don't need using statements if you have the using namespace at the beginning.

10. This is the only function that is allowed to do in/output. And I think this is the most complex of all the functions. You can not open a file from a url directly, only use local files. You can use ifstream for input file stream and ofstream for output. If you cannot open the file you must exit your function. Don't continue reading or writing on an invalid stream.
Logically split your function in three parts. 1. Read in the file into a string. Just append single to a string. 2. Call the modifier functions you are supposed to call. 3. Write the string back to the output file.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Where to start

Postby Hydrokr0n1k » Thu Apr 26, 2012 12:24 am

ok so then from what I read your pass compiler was
Code: Select all
#include <conio.h>
#include <iostream>
#include <cstring>
#include <string>
#include <string.h>
using namespace std;

std::string getPassword()
{

    std::string password = "";
    char c = getch();
    while (c != '\n' && c != '\r') {
        if(c==8) {
            if(password.length()>0) {
                password = password.substr(0, password.length() - 1);
                cout << "\b \b";
            }
        } else {
            password += c;
            cout << "*";
        }
        c=getch();
    }
    cout << endl;

    return password;
}
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby Hydrokr0n1k » Thu Apr 26, 2012 12:27 am

ok and switch code 8 and 9
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby Hydrokr0n1k » Thu Apr 26, 2012 12:36 am

and with 9 you said delete the first three lines but still does not work but I tried it alone to make it work but still nothing
Code: Select all
#include <iostream>
using namespace std;
int ASCIIsumOfString(string s){
int sum = 0;
for(int i=0; i<s.length(); i++){
    sum += s[i];
}
return sum;}


int ASCIIsumOfStringArray(string *s, int numberOfStrings){
    int sum = 0;
    for(int i=0; i<numberOfStrings; i++){
        sum += ASCIIsumOfString(s[i]);
    }
    return sum;
}
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby Hydrokr0n1k » Thu Apr 26, 2012 12:38 am

as well I couldnt get your pass function to work sorry maybe something I did when copying it I appreciate all your help
Last edited by Hydrokr0n1k on Thu Apr 26, 2012 11:22 pm, edited 1 time in total.
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby Hydrokr0n1k » Thu Apr 26, 2012 12:40 am

also forgot to say 6 , 7, 8 work fine except when they are all in the same .cpp file is there a reason for this? can they not all ask for input when together?
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby exomo » Thu Apr 26, 2012 3:52 am

Please don't make a new post for every sentence. You can use the edit function if you want to add something.

And if you have problems with the code post the complete code that doesn't work so I can test it (or somebody else, in the unlikely event that there is anybody reading this). I don't know a reason why you shouldn't be able to use multiple functions that ask for input, so it looks like you are doing something wrong. But I have no idea what the problem is if you don't post the code.
My password input function (it's not a compiler :D) worked for me. Beside the fact that you have added some unnecessary headers it should also work for you.
Correct is the <string> header, you need it somewhere in you program. <string.h> is the header that is used in C programs to include the c string functions, it can't be used in c++ programs. (well, it works for most c++ compilers because of backward compatibility, but you should not use it) <cstring> is mostly the same as string.h, it is used if you want to use c string operations in a c++ program. But you don't need it for my code.
(And you need the iostream, but usually you have this one in your program anyway)
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden

Re: Where to start

Postby Hydrokr0n1k » Fri Apr 27, 2012 6:18 pm

Ok this is the code I have now sorry for not posting it :)
Code: Select all
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include<stdio.h>
#include <string.h>
using namespace std;
int main()
{
int doubledelete();
int repeat();
int numberwordsinstring();
int reverseloop;
int reversenoloop();
int asciistring();
int acsiiArraysum();
int getput();
}

// Part 1
//1. Find Number in Middle and return it
std::string middleCharacters(const std::string &str)
{
    if (str.length() <= 0) return ""; // For an empty string, return an empty string (customize this as desired)
    return str.substr((str.length() - 1) / 2, 2 - str.length() % 2);
}

//Part 2 Repeater

std::string repeat( const std::string &word, int times ) {
   std::string result ;
   result.reserve(times*word.length()); // avoid repeated reallocation
   for ( int a = 0 ; a < times ; a++ )
      result += word ;
   return result ;
}

int repeat() {
   std::cout << repeat( "Ha" , 5 ) << std::endl ;
   return 0 ;
}

// Part 3
//   loop through the string looking for ".  "
//   when ".  " is found, delete one of the spaces
//   Repeat process until ".  " is not found. 

string forceSingleSpaces1 (string str) {
    size_t found(str.find(".  "));
    while (found !=string::npos){
        str.erase(found+1,1);
        found = str.find(".  ");
    }

    return str;
}

int doubledelete(){

    cout << forceSingleSpaces1("sentence1.  sentence2.  end.  ") << endl;

    return EXIT_SUCCESS;
}

// Part 4 number of words in string

int numberwordsinstring()
{
   int i, numspaces;
   char nextChar;
   string msg;

   numspaces=1;

   cout << "Type in a string\n";
   getline(cin, msg);

   // checks each character in the string
   for (i=0; i<int(msg.length()); i++)
   {
      nextChar = msg.at(i); // gets a character
      if (isspace(msg[i]))
         numspaces++;
   }
   cout << "\nThere are " << numspaces << " words in this string.";
   cin.ignore();
   return 0;
}
// Part 6 Reverse


char STOP[265];

void reverse(char* a)// error: initializing argument 1 of ‘int reverse(char*)’ [-fpermissive]

{
int c = strlen(a) - 1;
for (; c >= 0; c--)
{
cout << a[c];
}
}
int reverseloop()
{

cout<<"Please digit an input to be reversed: "<<endl;
cin.getline(STOP,265);

reverse(STOP); //error: invalid conversion from ‘char’ to ‘char*’
// [-fpermissive]

system("pause");
return 0;
}
//7. Reverse no loops


int complexReverseString(string userInput)
{
   string source(userInput);
   string target( source.rbegin(), source.rend() );
   cout << "The reversed string is " << target << endl;

   return 0;
}
int reversenoloop()
{
   ifstream input;
   string forward;

   cout << "Please enter a string to see its reverse. (Will display reverse twice)" << endl;
   input.open("information.txt");
   cin >> forward;
   //reverseString(forward, findStringSize(forward)); a function I'm not actually calling
   input >> forward;
   complexReverseString(forward);
   
   input.close();

   system ("pause");

}
//8 Ascii
//C program to accept a string from user and
//display its ascii value and
//then display sum of all ascii value of strings


int asciistring() {

char String[100];
int Sum,Index;
Sum=0; //Sum is initially zero

printf("Enter the string:\n");
gets(String); //Accept String from User

for(Index=0;Index<strlen(String);Index++)
{
Sum+=(String[Index]); //Adds (the ASCII values of) the String characters.
}

printf("The sum is %d\n",Sum); //Printing it as %d gives the equivalent ASCII value.

return 0;

}
//9 acsiiArraysum
using std::cout;
using std::endl;

int acsiiArraysum()
{
const int myArrayNumberOfElements(5);
double myArray[myArrayNumberOfElements] = {1.1, 4.5, 5.7, 7.9, 10};
double sum(0);
for (int i(0); i < myArrayNumberOfElements; i++)
{
sum +=myArray[i];
}

cout << endl << "Sum of all elements: " << sum << endl;

system("PAUSE");
return 0;
}
//10 getput

int getput()
{
   string strdata = "pap.txt";
   fstream streamObject("pap.txt");
   fstream localFile("pap.txt");
   fstream newFile("new.txt");
        FILE *filepointer;
        int character;
        filepointer=fopen("pap.txt", "r"); /* filepointer points to data.txt */
        if (filepointer==NULL) { /* error opening file returns NULL */
                printf("Could not open data.txt!\n"); /* error message */
                return 1; /* exit with failure */
        }
        /* while character is not end of file */
        while ((character=fgetc(filepointer)) != EOF) {
                putchar(character); /* print the character */
        }
        fclose(filepointer); /* close the file */
        return 0; /* success */
}

this is my debug report
Code: Select all
1>------ Build started: Project: string, Configuration: Debug Win32 ------
1>Build started 4/27/2012 6:12:39 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\string.unsuccessfulbuild".
1>ClCompile:
1>  strings.cpp
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(15): warning C4101: 'reverseloop' : unreferenced local variable
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(158): warning C4996: 'gets': This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          e:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(277) : see declaration of 'gets'
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(160): warning C4018: '<' : signed/unsigned mismatch
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(199): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          e:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
1>e:\users\hk3008\documents\visual studio 2010\projects\string\string\strings.cpp(144): error C4716: 'reversenoloop' : must return a value
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.36
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I still have not put the pass word function in yet trying to ge tit to work on its own first then will put in part 5 or 6 I think sorry I know its alot of code but thank you for your help :)
Hydrokr0n1k
 
Posts: 11
Joined: Wed Apr 11, 2012 11:51 pm

Re: Where to start

Postby exomo » Mon Apr 30, 2012 6:23 am

Code: Select all
int main()
{
int doubledelete();
int repeat();
int numberwordsinstring();
int reverseloop;
int reversenoloop();
int asciistring();
int acsiiArraysum();
int getput();
}

Are those supposed to be prototypes or function calls? The prototypes are usually at global scope. If they are supposed to be function calls you don't write int in front of the function names. You probably want both, the prototypes outside of the main function and call the functions in your main(). (and there are () missing at reverseloop, that is why you get the first warning). You actually can have the prototypes inside a function, but this is usually not what you want.

Second warning: gets() is considered unsafe because there is no protection from bufferoverflow. Whatever the user enters is copied to memory, but this can easily go out of your buffer. There are better functions you can use for C code, but you program is C++, so you better don't use C i/o functions at all. When you copy code you should at least adjust it to the language you use. use getline() and cout<< instead of gets and printf()

Third warning: Comparison of signed and unsigned numbers can cause errors that result in unexpected behaviour because the comparison does not evaluate the way you think it does. In your case this won't be a problem because the numbers are in a "good range". You can make the warning go away when you declare Index as unsigned int.

Now about the getput() function:
You are to use the fstreams. fopen, fclose, fgetc are C functions again. As stated in the assignement text you are supposed to use get and put, they are members of fstream.

And finally the one that gives you the error:
You have defined reversenoloop to return a int, but you have no return statement. Either add return 0; (or any other value you like) at the end of the function or just declare the function to return void. It seems you don't need the return value, so the latter is the better option.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to For Beginners

Who is online

Users browsing this forum: No registered users and 0 guests