2 Replies - 3240 Views - Last Post: 01 July 2013 - 04:15 AM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 07-October 12

Problem with string formatting not showing percent sign

Posted 01 July 2013 - 03:59 AM

I have a problem with string formatting, since C++ would read my last percent sign as modulus. Is there a way that I can show a percent sign after my second integer value? The problem occurs at line 18, where it is not showing the percent sign. Any help would be appreciated.

#include <iostream>
#include <stdio.h>
using namespace std;

int main(){
    int mark, x, counter;
    counter = 0;
    cout << "How many students would you like to add" << endl;
    cin >> x;
    int mark_list[x];
    while (counter != x){
        cout << "Student " << counter+1 << endl;
        cin >> mark_list[counter];
        counter++;
    }
    for (int a = 0; a != counter; a++){
        //cout << "Student " << a+1 << ": " << mark_list[a] << "%" << endl;
        printf("Student %d: %d%\n", a+1, mark_list[a]);

    }
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with string formatting not showing percent sign

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Problem with string formatting not showing percent sign

Posted 01 July 2013 - 04:09 AM

Because the % sign is used for format arguments, you need two to print an actual % sign.

printf("Student %d: %d%%\n", a+1, mark_list[a]);

Was This Post Helpful? 1
  • +
  • -

#3 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 07-October 12

Re: Problem with string formatting not showing percent sign

Posted 01 July 2013 - 04:15 AM

Thank you a lot JackOfAllTrades
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1