我如何在c++中使用带有字符串数组的for循环

How can i use for loop with String Array in c++

本文关键字:字符串 数组 循环 for c++      更新时间:2023-10-16

我在编译上述源代码时发现了错误。我想让循环到字符串的数组随着numberer数组的增加而增加。像

1 + st = 1st;
2 + nd = 2nd;
 3 + rd = 3rd;

我的代码低于

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
int main(){
int value=1;
int ivalue;
int sum=0;
int average;
int x,y;
int count=0;
string A[5]={"st","nd","rd","th","th"};

cout << "Enter loop limit : "; cin >> value ;
cout<<endl;
cout<<endl;
for( x=0;x<=value-1;x++){
    for (x=0;x<=A[5];x++)
        cout << "Enter "<<x+1<<A[0]<<" value : "; cin >> ivalue;
        sum=sum+ivalue;
        count++;


  }

以下代码适用于您吗?

int main(){
    int value=1;
    int ivalue;
    int sum=0;
    int count=0;
    string A[5]={"st","nd","rd","th","th"};

    cout << "Enter loop limit : "; cin >> value ;
    cout<<endl;
    cout<<endl;
    for(int x = 0; x <= value - 1; x++){
        cout << "Enter "<<x+1<<A[x]<<" value : "; 
        cin >> ivalue; 
        sum=sum+ivalue;
        count++;
    }
    cout<<"Sum:"<<sum<<endl;
}