Home > front end >  How to structure my for-loop correctly so my program runs?
How to structure my for-loop correctly so my program runs?

Time:09-03

So i have a function, string update_name(string names[], int size), which I wanted to let the user pick one of the three names they previously inputted, and change one of the name (picked by the user). However, when I try to run this program, it does ask me which name i wish to change, i type a name, but then it goes to the print_summary function again (as seen by the main function at the bottom). Not sure how to fix this. Thnanks

The function is the second last one in the code, just before the int (main), i put *** between it to help make it clearer.

 #include "splashkit.h"
 #include <vector>
 using std ::vector;
 #define SIZE 3

string read_string(string prompt)
 {
string result;
write(prompt);
result = read_line();
return result;
 }


 int read_integer(string prompt)
 {
string line;
int result;
line = read_string(prompt);
result = convert_to_integer(line);
return result;
 }


 double read_double(string prompt)
 {
string line;
double result;
line = read_string(prompt);
result = convert_to_double(line);
return result;
 }


 int total_length(string names[], int size)
 {
int result = 0;
    for (int i = 0; i < size; i  )
    {
    string name = names[i];
    result  = name.length();
    }
return result;
 }


  bool contains(string names[], int size, string name)
 {
  for (int i = 0; i < size; i  )
    {
    if (to_lowercase(names[i]) == to_lowercase(name))
        {
        return true;
        }
    }
return false;
 }


 string shortest_name(string names[], int size)
 {
string min;
min = names[0];
for (int i = 1; i < size; i  )
{
    if (min.length() > names[i].length())
    {
    min = names[i];
    }
 }
return min;
}


 string longest_name(string names[], int size)
 { 
string max;
max = names[0];
    for (int i = 1; i < size; i  )
    {
        if (max.length() < names[i].length())
        {
        max = names[i];
        }
    }
 return max;
 }


 int index_of(string names[], int size, string name)
 {
 int blah = 0;
    for (int i = 0; i < size; i  )
    {
    if (to_lowercase(names[i]) == to_lowercase(name))
        {
        blah = i   1;
        }
    }
    if (blah == 0)
    {
    blah = blah -1;
    }
 return blah;
 }


 void print_summary(string names[], int size)
 {  
 write_line(" ");
 write_line(" ~ Names List ~ ");
 write_line(" ");

 for (int i = 0; i < size; i  )
    {
    write_line(names[i]);
    }

 write_line(" ");
 int total;
 total = total_length(names, size);
 write("The total length is ");
 write_line(total);
 write_line("The shortest name is "   shortest_name(names, size));
 write_line("The longest name is "   longest_name(names, size));

 bool has_me;
 has_me = contains(names, size, "John");

 if (has_me)
    {
    write("This list contains the name John, at the index of: ");
    int index = index_of(names, size, "John");
    write_line(index);
    }
 else write("This list does not contain the name John");

 write_line(" ");

  }

  *************************
 string update_name(string names[], int size)
 {
 string name_change;
 string result;

  write_line(" ");
  name_change = read_string("Choose a name you wish to update (enter 
   the name): ");

  for(int i=0; i;)
    {
    if(to_lowercase(name_change) == to_lowercase(names[i]))
        {
        string newname = read_string("What is the new name?: ");
        names[i]=newname;
        break;
        }
    }

 return result;
 }
 *****************************

 int main()
 {

#define SIZE 3
string names[SIZE];
string newname;
string data[SIZE];
int i;

i = 0;

    while (i < SIZE)
    {
    names[i] = read_string("Enter a name: ");
    i  ;
    }   

print_summary(names, SIZE);
update_name(names, SIZE);
print_summary(names, SIZE);
write_line(" ~ Goodbye! ~ ");
write_line(" ");

return 0;

 }

The problem lies with the for(int i=0; i;) but I cannot figure out what the problem is and how to fix it.

CodePudding user response:

To fix your for-loop you need to have a correct condition statement and increment expression. To loop from 0..=3 you would structure the for-loop as:

for (int i = 0; i < 4;   i)
{ ... }

In general:

  • You have a init-statement that initializes a variable that will exist only for the scope of the for-loop. eg: int i = 0 - An integer i initialized to 0.
  • A condition statement that ensures some condition is true and breaks out of the loop once it no longer holds. eg: i < 4 - While i is less-than 4.
  • And finally, a increment expression that is run at the end of each iteration in the loop. eg: i - Increment i by 1.

Generic structure of a for-loop

for (init-statement; condition; increment-expression)
{ ... }

See: for-loops

  •  Tags:  
  • c
  • Related