Pointer and string trouble on programing assignments from my CSCI class.

I have a few programing assignments from my CSCI class that have been giving me a lot of trouble. I would be much obliged if someone can point me in the right direction or give me some sample code that will help me solve these programs. I'm pretty basic with regards to knowing C coding as we are working with Microsoft's Visual C++ compiler but this class is the second C++ class at my college that students take.

Here is what my 1st assignment asks.

1. Password Verifier
Create a New Project called YourLastnamePassword.

Imagine you are developing a software package that requires users to enter their own passwords. Your software requires that users’ passwords meet the following criteria:

The password should be at least ten characters long.
The password should contain at least two uppercase and at least one lowercase letter.
The password should have at least one digit.

Write a program that asks for a password and then verifies that it meets the stated criteria. If it doesn’t, the program should display a message telling the user why.


This is what I have so far come up with:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;


int main()
{
const int size=10;
string pass;
int length;
int caps=0;
int num=0;
int low=0;
cout << "Please enter a 10 character password.\n";
cout << "You must make sure your password has at\n";
cout << "least two uppercase and at least one\n";
cout << "lowercase letter and atleast 1 number.\n";
cin >> pass;
length = sizeof(pass);
cout << length <<endl;
while (length != 10)
{
cout << "You can only enter 10 characters. Please type again.\n";
cin >> pass;
length = sizeof(pass);
}
for (int cnt=0; cnt<size; cnt++)
{
cout << pass[cnt] << " ";
}
// New line
cout << endl;
for (int i=0; i<size; i++)
{
cin >> pass[i];
if (isdigit(pass[i]))
num=num+1;
if (isupper(pass[i]))
caps=caps+1;
if (islower(pass[i]))
low=low+1;
}
while (caps<2 || low < 1 || num < 1)
{
num=0;
caps=0;
low=0;
cout << "You must make sure your password has at\n";
cout << "least two uppercase and at least one\n";
cout << "lowercase letter and atleast 1 number.\n";
cin >> pass;
for (int i=0; i<size; i++)
{
cin >> pass[i];
if (isdigit(pass[i]))
num=num+1;
if (isupper(pass[i]))
caps=caps+1;
if (islower(pass[i]))
low=low+1;
}
}
cout << "Your password: " << pass << ", is good and excepted.\n";


return 0;
}


My big problem here is that sizeof(pass) keeps coming out to equal 32 even no matter what or how many characters are typed in. Because of this problem I can't really check the rest of the code to see if it works.


My second problem is with this assignment:

1. Array Expander
Create a New Project called YourLastnameExpander.

Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function in a complete program. Print the contents of the array inside the main function.


Because I am not so sure how to use pointers along with the 'new' function I thought I might start off on a small part of the code so I can get a better hang of these codes.

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
int asize=4;
int *ptr;
ptr= new int(asize);
ptr[0]=4;
ptr[1]=3;
ptr[2]=7;
ptr[3]=4;
for (int c=0; c<4; c++)
{
cout << ptr[c];
}

return 0;
}





Unfortunately every time I try to run the code it crashes the program even though the compiler doesn't detect any errors. This assignment is probably gonna be one that I need to look at another programming code that is similar to it so I can see what I did wrong.

These last two assignments are ones that I definitely need to see and a similar
program code in order to understand how to work them out especially the last one.


3. Pig Latin
Create a New Project called YourLastnamePigLatin.

Write a program that reads a sentence as input and converts each word to “Pig Latin”. In one version, to convert a word to Pig Latin you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word.

Here is an example:
English: I SLEPT MOST OF THE NIGHT

Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY



4. Mode Function
Create a New Project called YourLastnameModeFunction.

In statistics, the mode of a set of values is the value which occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:

A. An Array of integers
B. An integer that indicates the number of elements in the array

The function should determine the mode of the array. That is, it should determine which value in the array occurs most often. The mode is the value the function should return. If the array has no mode (none of the values occur more than once), the function should return -1. (Assume the array will always contain non-negative values.)

Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.


Anyhelp would be much appreciated. Thank you.
Problem #1: strings have a member function called length...
Problem #2: ptr = new int(asize); should be ptr = new int[asize];
Problem #3: You should get one word per time, modify it and store it in a new string
Problem #4: I suggest you creating a class storing the value and the times that value occurred
Last edited on
I'm new to C++ and these forums, but am trying to learn more on my own by working out problems that other people post here.

I have been trying to do a simplified version of the above Pig Latin assignment but am having some problems. To start, I've been focusing on writing a code that takes just one word and translates it to Pig Latin.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()

{
    
    char word[5];
    cout << "Enter word to be translated\n";
    cin >> word;
    cout << "You entered " <<word <<".\n"
         << "The translation is: ";


    for (int i=1;i<=5;i++)
    {
        cout << word[i];
    }
             
    cout << word[0] <<"ay\n";
    

    
    system ("PAUSE");
    return 0;
    
}


This works, but only if the word happens to be 6 characters in length. I've tried things like

1
2
    int x;
    char word[x];


to allow for other lengths, but this doesn't seem to work. Any suggestions?
Use C++ strings:
1
2
3
#include <string>
using namespace std;
string cppstring = "This is a Cpp string and it could have any length";

or dinamic allocation:
1
2
int x;
char* word = new char[x];
Thanks for the reply Bazzy.

I've tried using strings as well:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    string word;
    cout << "Enter word to be translated\n";
    cin >> word;
    cout << "You entered " << word <<".\n"
         << "The translation is: ";

    for (int i=1;i<10;i++)
    {
        cout << word[i];
    }
             
    cout << word[0] <<"ay\n"; 
    
    system ("PAUSE");
    return 0;    
}


Here, I have i<10 as the for loop conditional, but I would like to insert "i<the number of letters in 'word'" in place of it. I feel as though the answer is simple, but I just can't figure out how to instruct the computer to find the number of letters. :\
Strings have a member function called .length() that returns the length of the string. You do realize you are basically cutting off the first letter and adding 'ay' right? I would just use the string member function .substr() in order to get a portion of the string.
I've only been studying C++ for two weeks now and haven't had the opportunity to get acquainted with different member functions yet.

Your suggestion led me to the library section of this site, though, where I found the "strlen" function, which solved my problem nicely.

Thanks for the help and I'll be sure to look into the substr string memeber function for future reference.
Topic archived. No new replies allowed.