how do you do a loop for this??

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.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

int main()
{
    int ahi;
    int hi;
    cin >> ahi;
    char name[ahi];
    cin >>name;
    while (ahi=hi)
    {
        
    if(name[ahi]=='a'){cout <<"1";}
    else if(name[ahi]=='b'){cout<<"2";}
    else if(name[ahi]=='c'){cout<<"3";}
    else if(name[ahi]=='d'){cout<<"4";}
    else if(name[ahi]=='e'){cout<<"5";}
    else if(name[ahi]=='f'){cout<<"6";}
    else if(name[ahi]=='g'){cout<<"7";}
    else if(name[ahi]=='h'){cout<<"8";}
    else if(name[ahi]=='i'){cout<<"9";}
    else if(name[ahi]=='j'){cout<<"10";}
    else if(name[ahi]=='k'){cout<<"11";}
    else if(name[ahi]=='l'){cout<<"12";}
    else if(name[ahi]=='m'){cout<<"13";}
    else if(name[ahi]=='n'){cout<<"14";}
    else if(name[ahi]=='o'){cout<<"15";}
    else if(name[ahi]=='p'){cout<<"16";}
    else if(name[ahi]=='q'){cout<<"17";}
    else if(name[ahi]=='r'){cout<<"18";}
    else if(name[ahi]=='s'){cout<<"19";}
    else if(name[ahi]=='t'){cout<<"20";}
    else if(name[ahi]=='u'){cout<<"21";}
    else if(name[ahi]=='v'){cout<<"22";}
    else if(name[ahi]=='w'){cout<<"23";}
    else if(name[ahi]=='x'){cout<<"69";}
    else if(name[ahi]=='y'){cout<<"24";}
    else if(name[ahi]=='z'){cout<<"25";}
    hi=hi++
    }
    return 0;
}





thanks!!!
Your code has:
1
2
3
4
5
6
7
8
9
10
11
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
  }

keskiverto wrote:
hi = hi++ // lacks semicolon and is not going to end this loop in foreseeable future

It might. It is undefined behavior, so hard telling. ;)
thanks,it worked!!
closed account (j3Rz8vqX)
Check in the loops section:
http://www.cplusplus.com/doc/tutorial/control/

An example:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstring>
using namespace 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 = new char[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.
Last edited on
Topic archived. No new replies allowed.