I'm a beginner and I have a program that turns names into codes but I just have to fix the loop. I don't know how to use the arry so that stumps me the most so I wish someone would help me fix this.
cin >> ahi;
char name[ahi]; // size is not known during compilation. Not in standard.
cin >> name; // Do you think that there is a ostream operator >> for a char array?
// What should it do?
// Use std::string instead
int hi; // uninitialized, value is unknown
while ( ahi=hi ) // Assignment of unknown value to ahi. Not an equality comparison.
{
hi = hi++ // lacks semicolon and is not going to end this loop in foreseeable future
}
#include <iostream>
#include <cstring>
usingnamespace std;
int main()
{
//Variable declarations:
char *name; //Pointer to char; our array.
char buffer[100]; //Array of characters; c string.
int strSize; //Integer to store the size of our array.
//Prompt user for Input:
cout<<"Enter the name (lowercase only): ";
cin.getline(buffer,100);
//Determine string length for manipulation (c string)
strSize = strlen(buffer);
//I prefer to use 'for loops' when using counter controlled loops, with comparisons:
cout<<"Printing "<<strSize<<" characters from buffer[100]: "<<'\n';
for(int i = 0; i<strSize; ++i)
cout<<(buffer[i]-'a')+1<<' '; //Only accepts lowercase values for now.
cout<<'\n';
//---------------------------Dynamic array printing---------------------------------
//Creating a dynamic string, to correlate with your size specific c string.
name = newchar[strSize];
//Copy data from buffer[100] to name[strSize]
for(int i=0;i<strSize;++i)
name[i]=buffer[i];
cout<<"Printing from name["<<strSize<<"]: "<<'\n';
for(int i = 0; i<strSize; ++i)
cout<<(name[i]-'a')+1<<' '; //Only accepts lowercase values for now.
cout<<'\n';
//Ensure to delete 'new' from name[strSize];
delete []name;
//----------------------------------------------------------------------------------
return 0;
}
The pointer c string was added to assist with your construction of dynamic arrays; dependent on your compiler, some may support 'extended c libraries' and may allow char name[ahi]; but it is not standard and will be flagged most compilers - ISO C99.
G++ supports a C99 feature that allows dynamically sized arrays. It is not standard C++. G++ has the -ansi option that turns off some features that aren't in C++, but this isn't one of them. To make G++ reject that code, use the -pedantic option
http://stackoverflow.com/questions/1204521/dynamic-array-in-stack
And "-pedantic-errors" to flag it as an error if you are receiving a warning.
The intention is to disable variances that would prohibit 'some compilers' the ability to compile your code; your choice - simply stating compatibility.